gpt4 book ai didi

php - 即使关闭模拟准备,准备好的语句限制也不起作用

转载 作者:行者123 更新时间:2023-11-29 08:08:43 24 4
gpt4 key购买 nike

我的查询

$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);

所以从我读到的内容来看,这应该有效,不是吗?

最佳答案

以下内容对我有用,但是很难给出明确的答案,因为没有提供完整的代码。

这是我在我自己的服务器上使用现有表进行测试的结果。

旁注: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>
';

?>

关于php - 即使关闭模拟准备,准备好的语句限制也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22157156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com