更新时PHP准备语句错误
我准备了以下PHP语句代码:更新时PHP准备语句错误,php,mysql,prepared-statement,Php,Mysql,Prepared Statement,我准备了以下PHP语句代码: /* Register a prepared statement */ if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = (rotation + 1) % 4 WHERE ref_id = ?')) { /* Bind parametres */ $stmt->bind_param('i', $i
/* Register a prepared statement */
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = (rotation + 1) % 4 WHERE ref_id = ?')) {
/* Bind parametres */
$stmt->bind_param('i', $item_number);
/* Execute the query */
$stmt->execute();
/* Bind results */
$stmt->bind_result($rotation);
/* Fetch it */
$stmt->fetch();
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terribly wrong' . $mysqli->error;
}
问题是我得到一个错误,说:
mysqli_stmt::bind_result():绑定变量的数量与第21行/bla/bla/database/update_settings_rotate.php中准备好的语句中的字段数量不匹配
第21行是绑定结果的地方
好吧,我猜这和旋转等于某个东西的事实有关,而不是以“?”的形式出现,它通常应该出现在预先准备好的语句中。我试图改变这一点,但仍然存在错误。嗯,我认为当它变为变量时,它不会知道旋转等于什么,否则它不会让我将参数绑定为整数,因为我正在除法。即使它永远是一个真实的数字。有什么想法吗?嗯
我想当它是一个update语句时,绑定结果会有一些问题,但我不知道如何才能以任何方式得到答案?有更好的方法吗?在更新查询中使用括号
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = ((rotation + 1) % 4) WHERE ref_id = ?')) {
试试这个
/* Register a prepared statement */
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = (rotation + 1) % 4 WHERE ref_id = :val')) {
/* Bind parametres */
$stmt->bind_param(':val', $item_number);
/* Execute the query */
$stmt->execute();
/* Bind results */
$stmt->bind_result($rotation);
/* Fetch it */
$stmt->fetch();
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terribly wrong' . $mysqli->error;
}
您的原始代码没有太多错误。我没有更改您的更新查询。 您遇到的问题是试图从“update”语句中读取更新的值 不是很漂亮的代码,我留下了一些调试代码。我对语法相当迂腐。所有的反勾号都是多余的。我没有使用所有的变量,比如'allOk'。我之前在调试/检查代码时使用过它们 它在WindowsXP上的PHP5.3.18和mysql 5.5.16上进行了测试和工作 它更新数据库,然后显示更新的值
<?php
/* Q22377771 :
/* */
// new connection
$db = new mysqli('localhost', 'test', 'test', 'testmysql');
// ref_id of row to update...
$item_number = 1;
// the update statement
$stmt = $db->prepare('UPDATE `house_room1` SET `rotation` = (`rotation` + 1) % 4 WHERE `ref_id` = ?');
echo '<br/>prepare error: ', $db->errno, ' : ', $db->error, '<br/>';
/* Bind parametres */
$stmt->bind_param('i', $item_number);
// execute the update..
if ($allOk = $stmt->execute()) {
if ($db->affected_rows >= 1) {
echo '<br/>record updated!<br/>';
}
else {
echo '<br/>record NOT updated!<br/>';
}
}
else{
var_dump($db->affected_rows, $stmt->affected_rows, $allOk. $db->sqlstate, $stmt->sqlstate);
echo 'update error', $db->errno, ' : ', $db->error, '<br/>';
}
/* Close statement */
$stmt->close();
unset($stmt);
/* -------------------------------------------------------
* Now read the row so that we can get the rotation value
*/
// result in here...
$rotation = -1;
// the query statement
$stmt = $db->prepare('select `rotation` FROM `house_room1` WHERE `ref_id` = ?');
echo '<br/>prepare error: ', $db->errno, ' : ', $db->error, '<br/>';
/* Bind parametres */
$stmt->bind_param('i', $item_number);
$allOk = $stmt->execute();
/* Bind results */
$stmt->bind_result($rotation);
/* Fetch it */
$stmt->fetch();
// show result...
var_dump('rotation : '. $rotation);
/* Close statement */
$stmt->close();
$db->close();
?>
SET rotation=((rotation+1)%4)@AbhikChakraborty这没有任何区别,但无论如何,谢谢。哦,另一条错误消息出现了严重错误。您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在第1行使用的正确语法:val为什么要更改ref_id值?我想问题在于旋转部分?还是?谢谢,这是一个很好的解决方案