【问题标题】:Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL QuerySQL 查询中出现意外的 T_CONSTANT_ENCAPSED_STRING 错误
【发布时间】:2011-07-17 01:41:45
【问题描述】:

我在以下 SQL 查询中收到意外的 T_CONSTANT_ENCAPSED_STRING 错误:

mysql_query (UPDATE 'wp_posts' SET 'post_status' = 'publish' WHERE 'post_id' = '$id');

你们能看出错误可能在哪里吗?

这里是完整的代码,以防有帮助:

    $key = 'feed';
    $post_ids = array(2263, 2249); 

    foreach ($post_ids as $id) {
    $feedurl = get_post_custom_values($key, $id);
    $feedurlstr = implode($feedurl);

    // Ignore - it determines whether feed is live and returns $result
    LiveOrNot($feedurlstr);

    if ( $result == "live" ) {
    mysql_query (UPDATE 'wp_posts' SET 'post_status' = 'publish' WHERE 'post_id' = '$id');
    }    
    elseif ( $result == "notlive" ) {
    mysql_query (UPDATE 'wp_posts' SET 'post_status' = 'draft' WHERE 'post_id' = '$id');
    }
    endif;
    }

【问题讨论】:

    标签: mysql wordpress encapsulation


    【解决方案1】:

    将您的 SQL 语句用引号括起来 - "

    mysql_query ("UPDATE 'wp_posts' SET 'post_status' = 'publish' WHERE 'post_id' = '$id'");
    

    【讨论】:

      【解决方案2】:

      mysql_query() 接受一个字符串。 PHP 正在寻找散布在字符串中的常量,这不是有效的 PHP 语法。

      您需要对字符串进行分隔,'" 是流行的选择,但也有 Heredoc 语法。

      Read more about strings in PHP.

      【讨论】:

      • 感谢 Alex - 感谢您的帮助!
      猜你喜欢
      • 2011-02-12
      • 2020-02-01
      • 2014-05-24
      • 2015-06-21
      • 2015-01-23
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      相关资源
      最近更新 更多