您的SQL语法有错误;请帮忙 :)

时间:2014-11-04 10:17:33

标签: php mysql wordpress

任何人都可以帮忙吗?继续得到这种类型的错误,我完全被卡住了,赞赏!

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在第1行的“WHERE comment_ID = 1”附近使用正确的语法

$wp_table = "wp_comments"; // define wordpress table name  

$gmt_offset = '-1'; // -8 for California, -5 New York, +8 Hong Kong, etc.

$min_days_old = 2; // the minimum number of days old
$max_days_old = 50; // the maximum number of days old

// connect to db
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);


$result = mysql_query("SELECT comment_ID FROM $wp_table") or die(mysql_error()); 
while ($l = mysql_fetch_array($result)) {
$post_id = $l['comment_ID'];
echo "Updating: $post_id <br>";

$day = rand($min_days_old, $max_days_old);
$hour = rand(0, 23);

$new_date = date( 'Y-m-d H:i:s', strtotime("-$day day -$hour hour") );  
$gmt_new_date = date( 'Y-m-d H:i:s', strtotime("-$day day -$hour hour -$gmt_offset hour") );

mysql_query("UPDATE $wp_table SET comment_date='$new_date', comment_date_gmt='$gmt_new_date', WHERE comment_ID='$post_id'")
or die(mysql_error()); 

}

echo "<hr>DONE!";

5 个答案:

答案 0 :(得分:1)

从WHERE子句

之前删除逗号

答案 1 :(得分:1)

额外的逗号,where更新后的查询低于

mysql_query("UPDATE $wp_table 
               SET comment_date='$new_date', 
                   comment_date_gmt='$gmt_new_date' //extra Comma removed
               WHERE 
                   comment_ID='$post_id'")

不使用mysql_* api已弃用,在wordpress中您可以使用$wpdb(此处为documentation for same),其安全性。感谢Nikola指出。

答案 2 :(得分:0)

UPDATE-Part和WHERE子句之间还有一个逗号

答案 3 :(得分:0)

尝试PDO,并保护你的$ _POST     

$gmt_offset = '-1'; // -8 for California, -5 New York, +8 Hong Kong, etc.

$min_days_old = 2; // the minimum number of days old
$max_days_old = 50; // the maximum number of days old

try {
    $stmt = $db->prepare('SELECT comment_ID FROM wp_comments');
    $stmt->execute();
    $results = $stmt->fetchAll();
    if ($stmt->rowCount() == 0) {
        echo "No data";
        die();
    }
    foreach ($results as $result) {
        $post_id = mysql_real_escape_string($l['comment_ID']);
        echo "Updating: $post_id <br>";

        $day  = rand($min_days_old, $max_days_old);
        $hour = rand(0, 23);

        $new_date     = date('Y-m-d H:i:s', strtotime("-$day day -$hour hour"));
        $gmt_new_date = date('Y-m-d H:i:s', strtotime("-$day day -$hour hour -$gmt_offset hour"));

        $stmt = $db->prepare('UPDATE wp_comments SET `comment_date`=:ndate, `comment_date_gmt`=:gmtdate WHERE `comment_ID`=:post');
        $stmt->bindValue(':ndate', $new_date, PDO::PARAM_STR);
        $stmt->bindValue(':gmtdate', $gmt_new_date, PDO::PARAM_STR);
        $stmt->bindValue(':post', $post_id, PDO::PARAM_STR);
        $stmt->execute();

    }
}
catch (PDOException $ex) {
    die($ex->getMessage());
}

答案 4 :(得分:-1)

从查询中删除逗号

mysql_query("UPDATE $wp_table SET comment_date='$new_date', comment_date_gmt='$gmt_new_date' WHERE comment_ID='$post_id'")