2012年10月6日 星期六

MySQL出現Commands out of sync; you can't run this command now‏錯誤

PHP使用 mysqli 操作資料庫時,
出現了Commands out of sync; you can't run this command now‏錯誤。
MySQL官方說明:http://dev.mysql.com/doc/refman/4.1/en/commands-out-of-sync.html


原因是使用了 bind_result 儲存查詢結果後,
沒有使用 free_result 釋放記憶體。


例如:
......
......
$stmt_A = $DB->prepare($sql_A);
$stmt_B = $DB->prepare($sql_B);
......
......
$stmt_A->execute();
$stmt_A->bind_result($test);
$stmt_A->fetch();
$stmt_A->free_result();//要加這行
......
......
$stmt_B->execute();
var_dump($stmt_B->error);//否則會出現Commands out of sync; you can't run this command now 錯誤

沒有留言:

張貼留言