Latest web development tutorials

PHP mysqli_fetch_array () function

PHP MySQLi Reference Manual PHP MySQLi Reference Manual

Fetch a row as numeric or associative array from the results:

<?php 
// 假定数据库用户名:root,密码:123456,数据库:w3big 
$con=mysqli_connect("localhost","root","123456","w3big"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

$sql="SELECT name,url FROM websites ORDER BY alexa";
$result=mysqli_query($con,$sql);

// 数字数组
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s : %s",$row[0],$row[1]);

// 关联数组
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s : %s",$row["name"],$row["url"]);

// 释放结果集
mysqli_free_result($result);

mysqli_close($con);
?>

Definition and Usage

mysqli_fetch_array () function Fetch row as an associative array, a numeric array, or both from the results.

Note: Field names returned by this function are case-sensitive.


grammar

mysqli_fetch_array( result,resulttype ) ;

参数 描述
result 必需。规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。
resulttype 可选。规定应该产生哪种类型的数组。可以是以下值中的一个:
  • MYSQLI_ASSOC
  • MYSQLI_NUM
  • MYSQLI_BOTH

technical details

return value: Returns an array of strings and read rows that match. If there are no more rows in the result set returns NULL.
PHP version: 5+


PHP MySQLi Reference Manual PHP MySQLi Reference Manual