Home  >  Article  >  Backend Development  >  mysqli 的多sql语句执行有关问题

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

WBOY
WBOYOriginal
2016-06-13 12:58:061039browse

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@gmail.com', 0);";<br />
	$sql .= "insert into login (user1, pass, email, type) values ('dengchao1', md5('dengchao123'), 'dengchao1@gmail.com', 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的结果集释放了,再进行查询

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn