我的查询
$stmt = $db->prepare('SELECT * FROM generalevent ORDER BY date DESC LIMIT ?, 25');
$stmt->execute(array( $limit ));
一直没有收到消息
exception 'PDOException' with message 'SQLSTATE[42000]:
Syntax error or access violation: 1064
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 '?, 25' at line 1'
in ../test.php:35
Stack trace: #0 ../test.php(35): PDO->prepare('SELECT * FROM g...') #1 {main}
我已经
了$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
所以根据我的阅读,这应该有效,不是吗?
答案 0 :(得分:2)
以下对我有用,但很难给出明确的答案,因为没有提供完整的代码。
以下是我在现有桌面上自己测试的内容。
旁注:在:limit
之后使用?
或LIMIT
两者都有效并且没有任何错误。
<?php
$mysql_username = 'xxx'; // for DB
$mysql_password = 'xxx'; // for DB
try {
$pdo= new PDO('mysql:host=localhost;dbname=database_name', $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
} catch (PDOException $e) {
exit( $e->getMessage() );
}
try {
$currPage = 2;
// $limit = 1;
$limit = $currPage * 25;
$sql = "SELECT * FROM animals LIMIT :limit,25";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$results = $pdo->query($sql);
// print_r($results);
} catch (PDOException $e) {
echo $e->getMessage();
}
$cols = $results->columnCount(); // Number of returned columns
echo 'Number of returned columns: '. $cols. '<br />';
echo '
<div align="center">
<center>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="10%" bgcolor="#99CCFF"><p align="center">ID</td>
<td width="33%" bgcolor="#99CCFF"><p align="center">Breed</td>
<td width="34%" bgcolor="#99CCFF"><p align="center">Species</td>
</tr>
';
foreach($results as $row) {
echo "<tr><td width=\"10%\"><p align=\"center\">" . $row['id'] . "</td>\n<td width=\"33%\">" . $row['name'] . "</td><td width=\"33%\">" . $row['species'] . "</td>";
}
echo "\n";
echo "</tr>";
echo '
</table>
</center>
</div>
';
?>