为什么我会收到这个错误?WordPress数据库错误:[查询为空]

时间:2012-09-09 作者:Robbiegod

I am using Wordpress 3.4.2, latest version! I have no plugins installed except the one that I am writing.

So Here\'s the deal, I\'m reading a json feed and i\'m trying to import its content into a table that i created in my wordpress database. I\'ve used a similar set of code in another plugin that I wrote and it works fine there. I do not understand why it does not work here.

When i run this different plugin that i am writing i am get the dreaded and oh so clear "WordPress database error: [Query was empty]".

Here is my code...

// Setup the array
$values = array();

foreach ($bugs as $bug) {
 $status_text = $bug[\'text\'];
 $tContent = htmlSpecialChars(html_entity_decode($status_text));
 $imported = 2;

 [ shortened to keep this post short, but other values are listed here. ]


  // for the values insert part
  $values[] = \'("\'.$tContent. \'", "\'.$imported.\'")\';
}

if(is_array($values)) {
   echo "<hr><strong>WHAT TO IMPORT?</strong><br/>";
   var_dump(implode(\',\', $values));
}

This output all of the values as expected. Everything is wrapped in parentheses and seperate by a comma.

Then i do this...

global $wpdb;
$table_name = $wpdb->prefix . "bugtbl_temp";

$sql = $wpdb->prepare (
    "INSERT INTO $table_name
    (`post_content`, `imported`)
    VALUES ". implode(\',\', $values) . " ON DUPLICATE KEY UPDATE imported = 1"
);
$wpdb->query($sql); // execute query


$wpdb->print_error(); // any errors?
$wpdb->last_query; // show the qry
$wpdb->flush(); // cleanup

And then when i run this function I get the "WordPress database error: [Query was empty]" and nothing is imported into my table.

I saw on here one other guy was having problems with $wpdb->prepare. Am I using it incorrectly?

3 个回复
最合适的回答,由SO网友:Robbiegod 整理而成

成功我得到了wpdb->准备最终工作,我很乐意在这里分享我的代码片段——希望其他人会发现这很有用。

My Situation

我试图从正在编写的插件中将数据从提要导入到自定义表中。一些网站甚至回复此帖子的人都鼓励我保护我的导入查询。然而,即使在阅读了抄本之后,我也无法正确地或根本无法做到这一点。

Noteworthy Mistakes

我做错的几件事是:

将所有值拉入数组。我根本不需要这样做prepare导入多行值。解决方案是使用一个wp db->每行准备。我将wpdb->prepare命令移到了foreach语句中。问题是,有时我会得到一个以上的结果,我想在同一时间导入,这就做到了。我永远不会有超过20个结果,所以这应该很好

A Solution

这可能不是唯一的方法,但这是我成功的方法。下面是最后一段工作代码。我在我的foreach声明中提到了这一点。希望有帮助。

foreach ($rows as $row) {

 // I\'ve omitted the part in my where i take all of the values from the query and define variables, but that part would happen here.
// Prepare query
$sql = $wpdb->prepare("
    INSERT INTO $tablename (`ijAuthorID`, `ijDate`, `ijDescription`, `ijTitle`, `ijSlug`, `ijPostType`, `ijImported`, `ijUKID`) 
    VALUES (%d, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE ijImported = 1", 
    $tAuthor, $tPubDate, $tContent, $tTitle, $tPostName, $tPostType, $tImported, $tID
);

// Execute query
$wpdb->query($sql);

 }
感谢那些回复我原来帖子的人。

SO网友:kaiser

我在这里看到另一个人$wpdb->prepare. 我是不是用错了?

尽你所能read in the Codex, 这是一个简单的否定。

你必须像这样使用它sprintf/printf(). 唯一的allowed inputs

字符串:%s %d

示例

$sql = $wpdb->prepare( 
     "
        INSERT INTO %s
        (`post_content`, `imported`)
        VALUES %s
        ON DUPLICATE KEY UPDATE imported = 1
     "
    ,"{$wpdb->prefix}bugtbl_temp"
    ,implode( \',\', $values )
);
免责声明:↑ 显示的查询并不一定意味着它会给您结果,仅仅因为您使用了$wpdb->prepare()

SO网友:andkrup

当我忘记在update语句中添加最后一个数字参数时,我也遇到了同样的错误;检查您的参数是否与替换标记匹配。

假设您有如下sql字符串:

$sql = "update mytable set column1 = %s where id  = %d;"
$wpdb->query($wpdb->prepare($sql, \'foobar\'));
解决方案显然是添加id

$id = 5;
$wpdb->query($wpdb->prepare($sql, \'foobar\', $id));

结束