w3resource logo


PHP mysql_fetch_array function

PHP : mysql_fetch_array() function

<<PreviousNext>>

Description

The mysql_fetch_array() is used to retrieve a row of data as an array from a MySQL result handle.

Version

(PHP 4 and above)

Syntax

mysql_fetch_array(result, result_type)

Parameters

Name Description Required/ Optional Type
result Refers to the resource return by a valid mysql query (calling by mysql_query() function). Required Resource
result_type The type of the result is an array.
Possible values :
MYSQL_ASSOC - Associative array
MYSQL_NUM - Numeric array MYSQL_BOTH - Both associative and numeric array
Default : MYSQL_BOTH
Optional Integer

Return value

An array containing one row of data, or FALSE if there are no more rows.

Value Type : Array.

Example :

<?php
$con = mysql_connect("localhost", "root", "mypass") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorials");
$result = mysql_query("select * from tutorials");
echo "<table border='1' style='border-collapse: collapse'>";
echo "<th>Name of theTutorial</th><th>Pages</th><th>Examples</th><th>Author</th>";
while ($row = mysql_fetch_array($result)) 
{
echo"<tr><td>".$row['name']."</td><td>".$row['no_of_pages']."</td><td>".$row['no_of_examples']."</td><td>".$row['author']."</td></tr>";
}
echo "</table>";
mysql_close($con); ?>

Output :

Name of the TutorialPagesExamplesAuthor
html10001000rd
css300250rd
sql500500bc
mysql500500bc
javascript800800rg
php700700rg

See also

PHP Function Reference

mysql_fetch_row()

mysql_fetch_assoc()

mysql_data_seek()

mysql_query()

<<PreviousNext>>