<?php
//对象创建
$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 <br />rows: ".$mysqli->affected_rows;
}
else
{
//$mysqli->close();
die('insert error <br />'.$mysqli->error.$mysqli->close());
}
if(!is_bool($res))
{
$res->free();
}
$sql = "select * from login";
$res = $mysqli->query($sql);
echo $mysqli->error."<br />";
if($res)
{
while($row = $res->fetch_row())
{
foreach($row as $key => $val)
{
echo "--$val";
}
echo "<br />";
}
}
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的结果集释放了,再进行查询