我通过PDO连接到MySQL数据库,但是当我尝试在PHP中执行SQL调用时,它失败,并显示下面的错误。当我回显SQL并在MySQL Workbench中运行它时,它将返回到我期望的结果。这是$prepared->errorInfo()
输出时失败:在PHP中PDO调用失败,但在MySQL Workbench中成功执行
Array (
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "5" at line 17
)
所以它看起来像什么是错用事先准备好的声明,并在execute
值传递给它,但我不知道是什么。除了告诉我同样的问题,http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_parse_error并没有真正的帮助。
function __construct($params = array()) {
$DB = new PDO('mysql:host='.$config['host'].';port='.$config['port'].';dbname='.$config['name'], $config['user'], $config['pswd']);
if (!$DB) {
throw new Exception('Could not connect to database');
}
$type = $params['topic'];
if (isset($params['topic']) && $params['topic'] !== '') {
$typeSlugSql = "SELECT id from topics WHERE slug = ? LIMIT 1";
$prepared = $DB->prepare($topicSlugSql);
$list = $prepared->execute(array($params['topic'])); //NOTE: this is working correctly and returns the ID I expect
if (!$list) {
throw new Exception('topic slug execute failed');
}
$row = $prepared->fetch(PDO::FETCH_ASSOC);
$topic = $row['id']; //id is correctly 6 at this point
}
//generate SQL query
$sql = "SELECT
articles.id AS id,
articles.slug AS slug,
articles.title AS title,
articles.headline AS headline,
articles.description AS description,
articles.keywords AS keywords,
articles.content AS content,
DATE(articles.published_date) AS date,
articles.created AS created
FROM
articles articles
WHERE
articles.published_date <= CURRENT_TIMESTAMP
AND (articles.published IS NOT NULL OR articles.published = 1)
ORDER BY
articles.published_date DESC LIMIT 0 , ?";
//run the query and get the feed data
$prepared = $DB->prepare($sql);
$items = $prepared->execute(array(5)); //this will make the SQL fail...
echo $sql;
if (!$items) {
print_r($DB->errorInfo()); //this prints the 'errorInfo' listed above
throw new Exception('execution error...');
}
$options = array('ellipsis' => '...', 'exact' => false, 'html' => false);
while ($row = $prepared->fetch(PDO::FETCH_ASSOC)) {
array_push($this->resources, new Resource($row['title'], $row['slug'], $row['date'], $this->cleanAndTruncate($row['content'])));
}
}
最奇怪的部分是$prepared->execute(array($params['type']))
工作正常,并返回我期待的ID。再一次,如果我复制/粘贴echo
'd SQL并将其替换为?
和5
,则它在MySQL Workbench中按预期工作。如果我做的:
$sql = "SELECT
articles.id AS id,
articles.slug AS slug,
articles.title AS title,
articles.headline AS headline,
articles.description AS description,
articles.keywords AS keywords,
articles.content AS content,
DATE(articles.published_date) AS date,
articles.created AS created
FROM
articles articles
WHERE
articles.published_date <= CURRENT_TIMESTAMP
AND (articles.published IS NOT NULL OR articles.published = 1)
ORDER BY
articles.published_date DESC LIMIT 0 , 5";
$prepared = $DB->prepare($sql);
$items = $prepared->execute();
它工作在PDO和MySQL工作台,但我需要的LIMIT
取决于传递的参数更改为搞清楚什么是打破这个有什么建议?
'FROM 文章articles'? –
这只是我使用多个表的语法的一部分,所以它更清晰地显示,“从文章文章,articles_topics at,articles_something_else ase',它根本不影响执行。 – WOUNDEDStevenJones
可能的重复[LIMIT关键字与MySQL准备语句](http://stackoverflow.com/questions/10014147/limit-keyword-on-mysql-with-prepared-statement) – Anthony