PHP 准备语句更新时出错

标签 php mysql prepared-statement

我有这个 PHP 准备好的语句代码:

/* 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():绑定(bind)变量的数量与第 21 行/blablabla/bla/database/update_settings_rotate.php 中准备好的语句中的字段数量不匹配

第 21 行是我绑定(bind)结果的地方。

好吧,所以我猜这与旋转等于某物而不是显示为“?”这一事实有关。通常应出现在准备好的声明中。我尝试更改它,但仍然存在错误。嗯,我认为当它更改为变量时,它不会知道旋转等于什么,否则它不会让我将参数绑定(bind)为整数,因为我正在除法。即使它永远是一个实数。有任何想法吗?嗯

我猜当它是更新语句时,绑定(bind)结果会出现一些问题,但我不知道如何以任何方式解决这个问题?还有更好的方法吗?

最佳答案

您的原始代码并没有太大错误。我没有更改您的更新查询。 您遇到的问题是尝试从“更新”语句中读取更新后的值。

不是漂亮的代码,我留下了一些调试代码。我对语法相当迂腐。所有的反引号都是多余的。我不使用所有变量,例如“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();
?>

关于PHP 准备语句更新时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22377771/

相关文章:

python - 使用应用程序的 sql 文件夹内的 sql 文件将初始数据提供到 sql 表中在 django1.9 中不起作用

php - PDO 和 MySQL 存储过程。不返回参数

php - 关联数组索引中的美元符号

php - Laravel 中 NPM、Composer 和 Bower 的区别?

php - Doctrine 十进制在查询中不存在

mysql - r Shiny 的日期范围输入到 sql 查询

php - 使用 INT 更新数据库表

PHP 准备语句准备第二条语句失败

java - php从android获取数组

php - 在表中搜索字符串和数字