I have this PHP Prepared statements code:
/* 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;
}
The problem is that I get an error saying: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in /blablabla/bla/database/update_settings_rotate.php on line 21
Line 21 is where I bind results.
Okay, so I guess it has something to do with the fact that rotation is equal to something and not appeared as a "?" which it usually shall be in prepared statements. I tried changing that but how there was still errors. hm I don't think it will know what rotation
is equal to when its changed to a variable, or else it wont let me bind the parameter as an integer since I am dividing. Even though it will always be a real number. Any ideas? hm
I guess there are some problems binding results when it's an update statement, but I don't know how I would get that out in any way? Are there any better ways?
在更新查询中使用方括号
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = ((rotation + 1) % 4) WHERE ref_id = ?')) {
Try this
/* 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;
}
Not much incorrect with your original code. I haven't changed your update query. The issue you had was trying to read the updated value from the 'update' statement.
Not pretty code, i have left some debugging code in. I was rather pedantic with the syntax. All the backticks are redundant. I do not use all the variables such as 'allOk'. I did use them earlier while i was debugging / checking the code.
It is tested and works on PHP 5.3.18, mysql 5.5.16 on windows XP.
It updates the database and then shows the updated value.
<?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();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.