mysqli 的多sql語句執行有關問題

來源:互聯網
上載者:User
mysqli 的多sql語句執行問題
//對象建立
$mysqli = new mysqli('localhost', 'root', 'nagiosxi','hrsystem');
//串連是否出錯
if($mysqli->connect_error)
{
die('connect database error'.mysqli_connect_error());
}
$sql = "insert into login (user, pass, email, type) values ('dengchao1', md5('dengchao123'), [email protected]', 0);";
$sql .= "insert into login (user1, pass, email, type) values ('dengchao1', md5('dengchao123'), [email protected]', 0)";
$res = $mysqli->multi_query($sql);
if($res && $mysqli->affected_rows > 0)
{
echo "insert ok
rows: ".$mysqli->affected_rows;
}
else
{
//$mysqli->close();
die('insert error
'.$mysqli->error.$mysqli->close());
}
if(!is_bool($res))
{
$res->free();
}

$sql = "select * from login";
$res = $mysqli->query($sql);
echo $mysqli->error."
";
if($res)
{
while($row = $res->fetch_row())
{
foreach($row as $key => $val)
{
echo "--$val";
}
echo "
";
}
}
if(!is_bool($res))
{
$res->free();
}
$mysqli->close();
?>

為啥會報錯:Commands out of sync; you can't run this command now
這句報錯,對應的查詢語句是
$sql = "select * from login";
$res = $mysqli->query($sql);

非常不解,請答案,沒百度到。
------解決方案--------------------
先把multi_query的結果集釋放了,再進行查詢
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.