owwyess
owwyess

Reputation: 175

Php save variable (globally?) from local

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.

Upvotes: 0

Views: 71

Answers (1)

segarci
segarci

Reputation: 737

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

Upvotes: 1

Related Questions