Home > Database > Mysql Tutorial > mysqli多查询特性 实现多条sql语句查询

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

WBOY
Release: 2016-06-07 17:55:07
Original
977 people have browsed it

mysqli相对于mysql有很多优势,mysqli连接数据库和mysqli预处理prepare使用,不仅如此,mysqli更是支持多查询特性

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

和 使用 。不仅如此,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()执行多条语句时,语句之间是用 ; 隔开的,否则会出现错误
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