简体   繁体   English

PHP UPDATE上的语句准备错误

[英]PHP prepared statement error on UPDATE

I have this PHP Prepared statements code: 我有此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;
            }

The problem is that I get an error saying: 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 问题是我收到一条错误消息: 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

Line 21 is where I bind results. 第21行是我绑定结果的地方。

Okay, so I guess it has something to do with the fact that rotation is equal to something and not appeared as a "?" 好的,所以我想这与旋转等于某事物而不是不显示为“?”有关。 which it usually shall be in prepared statements. 通常应在准备好的陈述中使用。 I tried changing that but how there was still errors. 我尝试更改它,但是仍然有错误。 hm I don't think it will know what rotation is equal to when its changed to a variable, or else it wont let me bind the parameter as an integer since I am dividing. 嗯,我不认为当将其更改为变量时, rotation将等于什么,否则,由于我正在除法,因此它不会让我将参数绑定为整数。 Even though it will always be a real number. 即使它始终是实数。 Any ideas? 有任何想法吗? hm

I guess there are some problems binding results when it's an update statement, but I don't know how I would get that out in any way? 我想当它是更新语句时,在绑定结果时会遇到一些问题,但是我不知道我将如何解决这一问题? Are there any better ways? 有没有更好的方法?

在更新查询中使用方括号

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

Try this 尝试这个

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

Not much incorrect with your original code. 您的原始代码并不太正确。 I haven't changed your update query. 我尚未更改您的更新查询。 The issue you had was trying to read the updated value from the 'update' statement. 您遇到的问题是尝试从“更新”语句中读取更新的值。

Not pretty code, i have left some debugging code in. I was rather pedantic with the syntax. 不是很漂亮的代码,我还留下了一些调试代码。我对语法非常痴迷。 All the backticks are redundant. 所有的反引号都是多余的。 I do not use all the variables such as 'allOk'. 我没有使用所有变量,例如“ allOk”。 I did use them earlier while i was debugging / checking the code. 我在调试/检查代码时确实使用过它们。

It is tested and works on PHP 5.3.18, mysql 5.5.16 on windows XP. 它已经过测试,可以在Windows XP的PHP 5.3.18,mysql 5.5.16上运行。

It updates the database and then shows the updated value. 它更新数据库,然后显示更新的值。

<?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