Home  >  Article  >  Backend Development  >  php+mysqli批量查询多张表数据的方法_PHP

php+mysqli批量查询多张表数据的方法_PHP

WBOY
WBOYOriginal
2016-05-31 13:17:41830browse

本文实例讲述了php+mysqli批量查询多张表数据的方法。分享给大家供大家参考。具体实现方法如下:

注意这里使用到了两个新的函数multi_query与store_result,具体代码如下:

代码如下:

//1、创建数据库连接对象
$mysqli = new MySQLi("localhost","root","123456","liuyan");
if($mysqli->connect_error){
 die($mysqli->connect_error);
}
$mysqli->query("set names 'GBK'");
//2、查询多个数据库表
$sqls = "select * from news limit 10,4;";
$sqls .= "select * from user;";
//3、执行并处理结果
if($res = $mysqli->multi_query($sqls)){
//注意:与$mysqli->query()不同,这里返回的是布尔值
 do{
  $result = $mysqli->store_result();//这里才真正返回结果集的资源对象,失败则返回false;
  while($row = $result->fetch_assoc()){
   foreach($row as $key=>$value){
    echo "--$value--";
   }
   echo "


";
  }
  $result->free();
  if($mysqli->more_results()){//判断是否还存在有结果集
   echo "----------查询下一张表的数据---------------
";
  }
 }while($mysqli->next_result());//next_result() 返回 true 或false;
}
//4、关闭数据库连接
$mysqli->close();
?>

希望本文所述对大家的php程序设计有所帮助。

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