
mysqli_fetch_all()函数从结果集中取得所有行作为关联数组,或数字数组,或二者兼有。
定义和用法
PHP result对象(mysqli_result类)表示MySQL结果,由SELECT或DESCRIBE或EXPLAIN查询返回。
mysqli_fetch_all函数的作用是:接受一个结果对象作为参数,检索给定结果对象中的所有行。
语法
mysqli_fetch_all($result, [$type]);
参数
| 序号 | 参数及说明 | 
|---|---|
| 1 | result(必需) 这是表示结果对象的标识符。 | 
| 2 | type(必需) 这是一个整数值,指定返回数组的类型。type的值是以下值之一- 
 | 
返回值
PHP mysqli_fetch_all()函数返回一个数组(关联或数字),该数组包含结果对象的行。
PHP版本
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
在线示例
以下示例演示了mysqli_fetch_all()函数的用法(面向过程风格),从结果集中取得所有行作为关联数组:
<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("创建表.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   print("插入记录.....\n");
   //检索表的内容
   $res = mysqli_query($con, "SELECT * FROM myplayers");
   //获取结果的所有行
   $rows = mysqli_fetch_all($res);
   print_r($rows);
   //结束语句
   mysqli_free_result($res);
   //关闭连接
   mysqli_close($con);
?>输出结果
创建表..... 插入记录..... Array ( [0] => Array ( [0] => 1 [1] => Sikhar [2] => Dhawan [3] => Delhi [4] => India ) [1] => Array ( [0] => 2 [1] => Jonathan [2] => Trott [3] => CapeTown [4] => SouthAfrica ) [2] => Array ( [0] => 3 [1] => Kumara [2] => Sangakkara [3] => Matale [4] => Srilanka ) )
在线示例
在面向对象风格中,此函数的语法为$result->fetch_all();。以下是面向对象风格中此函数的示例,从结果集中取得所有行作为关联数组:
<?php
   //建立连接
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("创建表.....\n");
   $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   //执行语句
   $stmt->execute();
   //检索结果
   $result = $stmt->get_result();
   //读取所有行
   $rows = $result->fetch_all();
   print_r($rows);
   //结束语句
   $stmt->close();
   //关闭连接
   $con->close();
?>输出结果
创建表..... Array ( [0] => Array ( [0] => Raju [1] => 25 ) [1] => Array ( [0] => Rahman [1] => 30 ) )
