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

2016 October 12 at 11:38 am Leave a comment

Kalau kita execute store procedure Mysql dalam pemograman PHP. Kemudian kita melanjutkan query selanjutnya maka kita akan mendapatkan error “Commands out of sync; you can’t run this command now“.

Berikut solusi yang didapat dari: http://php.net/manual/en/mysqli.store-result.php Kita harus menyisipkan command yang memanggil custom function ‘clearStoredResults($sConn);‘  untuk membersihkan hasil query sebelumnya sebelum melanjutkan execute query berikutnya.

$sConn = mysqli_connect($servdb, $usrdb, $usrdbpasswd);
if(!$sConn){
        echo "{msg:'Connection Error'}";
        exit;
}
$user='bangbang';
$db = mysqli_select_db ($sConn,$dbname);
$sql = "Call userlogin ('{$user}')";
$res=$sConn->query ($sql);
while($result = $res->fetch_array(MYSQLI_ASSOC)) {
        echo "Jumla baris=".$res->num_rows;
        echo    "<br>";
        clearStoredResults($sConn);
        echo    "<br>".$sql = "Update mtuser SET last_login=Now() Where iduser={$result['iduser']}";
        $sConn->query($sql);
        echo "<br>".mysqli_error($sConn);
}

#------------------------------------------
function clearStoredResults($mysqli_link){
#------------------------------------------
    while($mysqli_link->next_result()){
      if($l_result = $mysqli_link->store_result()){
              $l_result->free();
      }
    }
}


 

Entry filed under: MYSQL, PHP, RELIGI. Tags: .

Script Auto Backup Restore MYSQL Dua Server dengan Banyak Database

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories


%d bloggers like this: