欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

mysqli 的多sql语句执行有关问题

程序员文章站 2024-02-03 14:42:10
...
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的结果集释放了,再进行查询
mysqli 的多sql语句执行有关问题

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频