Home > Database > Mysql Tutorial > body text

mysqli multiple sql statement query

怪我咯
Release: 2017-03-31 10:13:35
Original
1885 people have browsed it

mysqli has many advantages over mysql. It is recommended that you use it. If you don’t understand it, you can check out the basic tutorial of mysql:
mysqliConnect to the database and use mysqli preprocessing prepare. Not only that, mysqli also supports multi-query features. Look at the following php code:

<?php 
$mysqli = new mysqli("localhost","root","","new");
$mysqli->query("set names &#39;utf8");
//多条sql语句
$sql = "select id,name from `user`;";
$sql .= "select id,mail from `user`";
echo $sql;
if ($mysqli->multi_query($sql)){//multi_query()执行一条或多条sql语句
	do{
		if ($rs = $mysqli->store_result()){//store_result()方法获取第一条sql语句查询结果
			while ($row=$rs->fetch_row()){
				var_dump($row);
				echo "<br>";
			}
			$rs->Close(); //关闭结果集
			if ($mysqli->more_results()){  //判断是否还有更多结果集
				echo "<hr>";
			}
		}
	}while($mysqli->next_result());//next_result()方法获取下一结果集,返回bool值
}
$mysqli->close();  //关闭数据库连接
?>
Copy after login

Regarding some of the methods used, I have annotated very clearly. Special attention should be paid to the fact that multi_query() executes multiple Statements are separated by ;, otherwise an error will occur


The above is the detailed content of mysqli multiple sql statement query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template