簡體   English   中英

PHP UPDATE上的語句准備錯誤

[英]PHP prepared statement error on UPDATE

我有此PHP Prepared語句代碼:

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

問題是我收到一條錯誤消息: 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

第21行是我綁定結果的地方。

好的,所以我想這與旋轉等於某事物而不是不顯示為“?”有關。 通常應在准備好的陳述中使用。 我嘗試更改它,但是仍然有錯誤。 嗯,我不認為當將其更改為變量時, rotation將等於什么,否則,由於我正在除法,因此它不會讓我將參數綁定為整數。 即使它始終是實數。 有任何想法嗎?

我想當它是更新語句時,在綁定結果時會遇到一些問題,但是我不知道我將如何解決這一問題? 有沒有更好的方法?

在更新查詢中使用方括號

if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = ((rotation + 1) % 4) WHERE ref_id = ?')) {

嘗試這個

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

您的原始代碼並不太正確。 我尚未更改您的更新查詢。 您遇到的問題是嘗試從“更新”語句中讀取更新的值。

不是很漂亮的代碼,我還留下了一些調試代碼。我對語法非常痴迷。 所有的反引號都是多余的。 我沒有使用所有變量,例如“ allOk”。 我在調試/檢查代碼時確實使用過它們。

它已經過測試,可以在Windows XP的PHP 5.3.18,mysql 5.5.16上運行。

它更新數據庫,然后顯示更新的值。

<?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();
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM