PHP mysqli_fetch_array() Function
In this chapter you will learn:
- Definition for PHP mysqli_fetch_array() Function
- Syntax for PHP mysqli_fetch_array() Function
- Parameter for PHP mysqli_fetch_array() Function
- Return for PHP mysqli_fetch_array() Function
- Example - fetches a result row as a numeric array and as an associative array
Definition
The mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both.
Syntax
mysqli_fetch_array(result,resulttype);
Parameter
Parameter | Is required | Description |
---|---|---|
result | Required. | Result set returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
resulttype | Optional. | What type of array to return. |
resulttype can be one of the following values:
- MYSQLI_ASSOC
- MYSQLI_NUM
- MYSQLI_BOTH
Return
It returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result-set.
Example
The following code fetches a result row as a numeric array and as an associative array.
<?php/*from ja v a 2 s. c o m*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT name,age FROM emp";
$result=mysqli_query($con,$sql);
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
print $row[0];
print "\n";
print $row[1];
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
print $row["name"];
print "\n";
print $row["age"];
mysqli_free_result($result);
mysqli_close($con);
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP mysqli_fetch_assoc() Function
- Syntax for PHP mysqli_fetch_assoc() Function
- Parameter for PHP mysqli_fetch_assoc() Function
- Return for PHP mysqli_fetch_assoc() Function
- Example - fetches a result row as an associative array
Home » PHP Tutorial » PHP MySQLi Functions