I should really know this by now, but I just can't figure it out anyway. Really weird because I thought I knew it but I just can't get it to work anyway. Well I have this code:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_POST['item_id'])){
$item_number = $_POST['item_id'];
require('../includes/db_connect.php');
/* Register a prepared statement */
if ($stmt = $mysqli->prepare('SELECT rotation FROM house_room1 WHERE ref_id = ?')) {
/* Bind parametres */
$stmt->bind_param('i', $item_number);
/* Execute the query */
$stmt->execute();
$stmt->bind_result($rotation);
while ($stmt->fetch()) { }
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terribly wrong' . $mysqli->error;
}
/* Register a prepared statement */
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = ? WHERE ref_id = ?')) {
/* Bind parametres */
$stmt->bind_param('ii', $i, $item_number);
$i = ($rotation + 1) % 5);
/* Execute the query */
$stmt->execute();
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terribly wrong' . $mysqli->error;
}
}
}
As you can see, I have this variable rotation which I get out from the database in the first SELECT statement. I need this value in the next query, UPDATE but the rotation variable is local I guess? So I can't reach it in the next query, how would I do this most effeciently? Thanks in advance.
nullor something, because there is no more data. But to be certain, just dump (or otherwise inspect) the variable and see what it contains, as @AmalMurali suggests.while ($stmt->fetch()) {}for single result row you can just call$stmt->fetch();. You also have a loose)at$i = ($rotation + 1) % 5);and I am not entirely sure but I think you could resume your query withUPDATE house_room1 SET rotation = ((rotation + 1) % 5) WHERE ref_id = ?UPDATE house_room1 SET rotation = (rotation + 1) % 5 WHERE id = ?;?