mysqli 的多sql语句执行问题
<?php<br /> //对象创建<br /> $mysqli = new mysqli('localhost', 'root', 'nagiosxi','hrsystem');<br /> //连接是否出错<br /> if($mysqli->connect_error)<br /> {<br /> die('connect database error'.mysqli_connect_error());<br /> }<br /> $sql = "insert into login (user, pass, email, type) values ('dengchao1', md5('dengchao123'), 'dengchao1@g<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong>mail.com', 0);";<br /> $sql .= "insert into login (user1, pass, email, type) values ('dengchao1', md5('dengchao123'), '[email protected]', 0)";<br /> $res = $mysqli->multi_query($sql);<br /> if($res && $mysqli->affected_rows > 0)<br /> {<br /> echo "insert ok <br />rows: ".$mysqli->affected_rows;<br /> }<br /> else<br /> {<br /> //$mysqli->close();<br /> die('insert error <br />'.$mysqli->error.$mysqli->close());<br /> }<br /> if(!is_bool($res))<br /> {<br /> $res->free();<br /> }<br /><br /> $sql = "select * from login";<br /> $res = $mysqli->query($sql);<br /> echo $mysqli->error."<br />";<br /> if($res)<br /> {<br /> while($row = $res->fetch_row())<br /> {<br /> foreach($row as $key => $val)<br /> {<br /> echo "--$val";<br /> }<br /> echo "<br />";<br /> }<br /> }<br /> if(!is_bool($res))<br /> {<br /> $res->free();<br /> }<br /> $mysqli->close();<br />?>
为啥会报错:Commands out of sync; you can’t run this command now
这句报错,对应的查询语句是
$sql = “select * from login”;
$res = $mysqli->query($sql);
非常不解,请答案,没百度到。
——解决方案——————–
先把multi_query的结果集释放了,再进行查询