mysql_fetch_row Function in PHP
What is mysql_fetch_row() function in PHP?
How does mysql_fetch_row works?
Explanation
mysql_fetch_row() function fetches a row of data from a result handle and returns it as an numerically keyed array.
Syntaxarray mysql_fetch_row ( resource result)
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
mysql_fetch_row() fetches one row of data from the resultset returned by
mysql_query(). The row is returned as an numerically keyed array.
To get the value from the array we have to use array offset starting from 0. Each call to this mysql_fetch_row function returns the next record. Here one record is returned at a time and returns false if there is no more record to return.
Example:
<?php // Attempt to connect to the default database server $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die ("Could not connect"); $select_db=mysql_selectdb("mydb"); //returns result set on executing the query $rst=mysql_query("select * from employee",$link); echo "id,name,salary<br>"; //fetches each row of result set as an array while($res=mysql_fetch_row($rst)){ //display the values using numeric array index echo "$res[0],$res[1],$res[2]<br>"; } ?> |
In the above code
mysql_query() executes a simple query and returns a resultset '$rst'. Resultset is passed as a parament to the mysql_fetch_row function to fetch each record of the resultset '$rst'.
mysql_fetch_row function returns record as a numerically indexed array '$res', it returns one record when each time it is called. This array can be accessed with the numeric array index starting from '0' to the length of the array.
RESULT:
Id name salary 1 John 6000
2 Alex 4000
3 Antony 9000
4 Mercy 4500