0

I'm getting the following error:

"Commands out of sync; you can't run this command now"

On this code:

$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);

The code looks correct, where I wrong?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
BlackSys
  • 91
  • 7

2 Answers2

1

That update query look very wrong. Try the following below,

Your old 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);

Your new $update,

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

LIMIT can be used with UPDATE but with the row count only.

Edit 1

You seem to be mixing OO MySQLi with procedural, please read this page.

Edit 2

You had quite a few issues with your code.

  1. You were trying to access $email and $key when they were out of the scope of the if so I added the new variables.

  2. You kept on (as said above) mixing your OO with procedural.

  3. I added some debugging when trying to execute $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>';
    }
    ?>
    
Community
  • 1
  • 1
Script47
  • 14,230
  • 4
  • 45
  • 66
  • @BlackSys now try it. – Script47 Aug 08 '15 at 16:49
  • I'm kinda confused, so what I have to do? Based on the page you linked me, isn't it the same behaviour of the two functions? edit: same error as above. I really don't get it, the syntax looks correct – BlackSys Aug 08 '15 at 16:51
  • tried it, however, i always get the error of out of sync. the syntax looks correct however, I really don't get it – BlackSys Aug 08 '15 at 16:53
  • It does not print the line error, but the first part of code, the $check one, is being executed correctly, or it will not pass the IF structure. so it must be the update query returning this error – BlackSys Aug 08 '15 at 16:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86516/discussion-between-script47-and-blacksys). – Script47 Aug 08 '15 at 16:56
0

wooo for a start you're mixing procedural and OOP, secondly you haven't closed your connections which is another error :)

$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();
}
bashleigh
  • 8,813
  • 5
  • 29
  • 49