Home  >  Article  >  Database  >  mysqli多查询特性 实现多条sql语句查询_MySQL

mysqli多查询特性 实现多条sql语句查询_MySQL

WBOY
WBOYOriginal
2016-06-01 13:23:361214browse

bitsCN.com

mysqli相对于mysql有很多优势,建议大家使用,如果没有了解,可以查看mysql的基础教程:

mysqli连接数据库 和 mysqli预处理prepare使用 。不仅如此,mysqli更是支持多查询特性,看下面这段php代码:

$mysqli = new mysqli("localhost","root","","new");
$mysqli->query("set names '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 "
";
}
$rs->Close(); //关闭结果集
if ($mysqli->more_results()){ //判断是否还有更多结果集
echo "


";
}
}
}while($mysqli->next_result());//next_result()方法获取下一结果集,返回bool值
}
$mysqli->close(); //关闭数据库连接
?>


关于其中用到的一些方法,我已经注释的很清楚,特别要注意的是multi_query()执行多条语句时,语句之间是用 ; 隔开的,否则会出现错误bitsCN.com
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