0

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.

8
  • " echo 'Something went terribly wrong' " Commented Mar 8, 2014 at 21:35
  • 1
    I think every fetch re-fills the bind parameter with a new value, including the last fetch, which ends the while loop. I think that last one might flush the variable and reset it to null or 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. Commented Mar 8, 2014 at 21:37
  • 3
    You don't need the 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 with UPDATE house_room1 SET rotation = ((rotation + 1) % 5) WHERE ref_id = ? Commented Mar 8, 2014 at 21:38
  • 1
    @owwyess check my comment again, you don't even need to make 2 queries. Commented Mar 8, 2014 at 21:45
  • 1
    Wouldn't it make the most sense to just say UPDATE house_room1 SET rotation = (rotation + 1) % 5 WHERE id = ?;? Commented Mar 8, 2014 at 21:47

1 Answer 1

1

You can declare $rotation as $rotation=null; after require statement. This way it will be available on the second query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.