使用简单查询的sql语法出错

时间:2011-09-30 20:34:14

标签: php mysql database mysqli

这是我的sql语句

SELECT ID, post_title, post_name, post_type 
  FROM `wp_posts` 
 WHERE `post_type` LIKE 'ep_e%'

我在phpMyAdmin中运行了这个sql语句,完美运行并返回106行(这里可能不重要)

但是在php中我使用数据库类运行此查询并且出现错误

  

您的SQL语法有错误;查看与MySQL对应的手册   服务器版本,用于在“ep_e%”附近使用正确的语法

我也试过插值但仍然出错:(

$evt = 'ep_event';
SELECT ID, post_title, post_name, post_type 
  FROM `wp_posts` 
 WHERE `post_type` like '{$evt}'

我真的很难过: - /并提前感谢。

这是我班级的功能

protected function _prepareQuery() 
{
    if (!$stmt = $this->_mysqli->prepare($this->_query)) {
        trigger_error("Problem preparing query ($this->_query) " . 
          $this->_mysqli->error, E_USER_ERROR);
    }
    return $stmt;
}

2 个答案:

答案 0 :(得分:1)

不确定这只是你帖子中的拼写错误,但是

select ID, post_title, post_name, post_type from `wp_posts` where `post_type` like {$evt}

应该是

select ID, post_title, post_name, post_type from `wp_posts` where `post_type` like '{$evt}'

注意最后的。你在phpMyAdmin中输入的内容是正确的,但你从代码中粘贴的内容没有。

答案 1 :(得分:1)

不知道是不是这样......

你在where子句中的'post_type'字段周围放置了刻度线,但是在字段选择中却没有...我不认为它是一个原本需要的保留字。您是否尝试删除所有刻度线,即使在表格中也是如此..

相关问题