【问题标题】:Mysqli Commands out of syncMysqli 命令不同步
【发布时间】:2015-10-31 23:48:18
【问题描述】:

我收到以下错误:

“命令不同步;您现在无法运行此命令”

关于此代码:

$check = $dbc->prepare("SELECT field FROM table WHERE field = ?");
mysqli_stmt_bind_param($check, "s", $value1);
mysqli_stmt_execute($check);
mysqli_stmt_bind_result($check, $info);
mysqli_stmt_fetch($check);

if ( $info == $static_value ) {

    $update = $dbc->prepare("UPDATE table SET field = 'valued' WHERE(field1 = ? AND field2 = ?)LIMIT 1");
    mysqli_stmt_bind_param($update, "ss", $value1, $value2);
    mysqli_stmt_execute($update);

代码看起来正确,我哪里错了?

【问题讨论】:

  • 你遇到了什么错误?
  • 对不起,我现在编辑添加更多信息,我得到的错误是:“命令不同步;您现在无法运行此命令”

标签: php


【解决方案1】:

update 查询看起来非常错误。请尝试以下操作,

你的旧update

$update = $dbc->prepare("UPDATE table SET field = 'valued' WHERE(field1 = ? AND field2 = ?)LIMIT 1");
mysqli_stmt_bind_param($update, "ss", $value1, $value2);
mysqli_stmt_execute($update);

你的新$update

$update = mysqli_prepare($dbc, "UPDATE `yourTableName` SET `field` = 'valued' WHERE field1 = ? AND field2 = ?");

LIMIT 可以与 UPDATE 一起使用,但只能与行数一起使用。

编辑 1

您似乎将OO MySQLiprocedural 混在一起,请阅读this 页面。

编辑 2

您的代码有很多问题。

  1. $email$key 超出if 的范围时,您尝试访问它们,因此我添加了新变量。

  2. 您继续(如上所述)将OOprocedural 混合。

  3. 我在尝试执行$update时添加了一些调试。

    <?php
    
    $email;
    $key;
    
    if (isset($_GET['email']) && preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $_GET['email'])) {
        $email = $_GET['email'];
    }
    if (isset($_GET['key']) && (strlen($_GET['key']) == 32)) //The Activation key will always be 32 since it is MD5 Hash
        {
        $key = $_GET['key'];
    }
    
    
    if (isset($email) && isset($key)) {
        $check_code = mysqli_prepare($dbc, "SELECT Activation FROM members WHERE Email = ?");
        mysqli_stmt_bind_param($check_code, "s", $email);
        mysqli_stmt_execute($check_code);
        mysqli_stmt_bind_result($check_code, $activation);
        mysqli_stmt_fetch($check_code);
    
        if ($activation == $key) {
    
            // Update the database to set the "activation" field to null
    
            $update = mysqli_prepare($dbc, "UPDATE `members` SET `Activation` = 'Activated' WHERE `Email` = ? AND `Activation` = ?");
            mysqli_stmt_bind_param($update, "ss", $email, $key);
            mysqli_stmt_execute($update);
    
            if (!mysqli_stmt_execute($update) ) {
                die("Error: " . mysqli_stmt_error($update));
            }   
    
    
            // Print a customized message:
            if (mysqli_affected_rows($dbc) == 1) { //if update query was successfull
    
                echo '<div class="success">Your account is now active. You may now <a href="login.php">Log in</a></div>';
    
            } else {
                echo '<div class="errormsgbox">Oops !Your account could not be activated. Please recheck the link or contact the system administrator.</div>';
                echo '<br/> ' . $dbc->error;
    
            }
    
            mysqli_close($dbc);
    
        } else {
    
            echo "Parameters wrong, wrong link?";
    
        }
    
    } else {
        echo '<div class="errormsgbox">Error Occured .</div>';
    }
    ?>
    

【讨论】:

  • @BlackSys 现在试试吧。
  • 我有点困惑,我该怎么办?根据您链接我的页面,这两个功能的行为是否相同?编辑:与上述相同的错误。我真的不明白,语法看起来正确
  • 试过了,但是总是出现不同步的错误。语法看起来是正确的,但是我真的不明白
  • 它不打印行错误,但是代码的第一部分,即 $check ,正在正确执行,否则它不会通过 IF 结构。所以它必须是返回此错误的更新查询
【解决方案2】:

首先你正在混合程序和 OOP,其次你还没有关闭你的连接,这是另一个错误:)

$check=$dcv->prepare('SELECT field FROM table where field=?');
$check->bind_param('s',$value1);
$check->execute();
$check->bind_result($info);
$check->fetch();
$check->close();
if($info==$static_value){
    $update=$dvc->prepare('UPDTATE table SET field='valued' WHERE field1=? AND field2=?');
    $update->bind_param('ss',$value1,$value2);
    $result=$update->execute();
    $update->close();
}

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 2013-10-05
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2020-05-07
    相关资源
    最近更新 更多