我通過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