PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_fetch_row Function in PHP


Tutorials »Php »

Topic

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.

Syntax
array 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








Other Links

web hosting