Php从本地保存变量(全局?)

时间:2014-03-08 21:33:40

标签: php

我现在应该知道这一点,但无论如何我都无法理解。真的很奇怪,因为我以为我知道它,但我无论如何都无法让它工作。好吧,我有这个代码:

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;
        }
    }
}

如您所见,我有这个变量rotation,我在第一个SELECT语句中从数据库中获取。我在下一个查询UPDATE中需要这个值,但旋转变量是本地的我猜?所以我无法在下一个查询中找到它,我将如何最有效地完成这项工作?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在$rotation声明后将$rotation=null;声明为require。这样它就可以在第二个查询中使用。