Click here to Skip to main content
13,096,858 members (84,997 online)
Rate this:
Please Sign up or sign in to vote.
See more:

i just want to asks about mysql_fetch_array( ) function
how does it works ?
i know that it retrieves an array
my question will be more clarify in the below example .

if (!$con)
  die ("error ".mysql_error());
$query="select * from emp";
while( $data=mysql_fetch_array($result))
echo $data[0].$data[1].$data[2].$data[3]

when i execute the above code , it works fine with no problem .
my question comes after i try the below code

$query="select * from emp ";
$data =mysql_fetch_array($result);
//now i try to get the record from DB record by record .
echo $data[0].$data[1].$data[2].$data[3]; //it will print the first row from emp table .
//here is my question , lets try to execute it again . 
$data =mysql_fetch_array($result);
echo $data[0].$data[1].$data[2].$data[3]; // it will display the second row from emp table and here is my question How and why ???

when i call this function for the second time : [ $data =mysql_fetch_array($result); ]
it should contain the whole table and therefore display the first row again
why it display the second one?

kindly i need a clarification for this , thanks :)
Posted 7-May-13 17:05pm
Mohibur Rashid 8-May-13 1:08am
First of it does not work the way you want it to work. It work its way.

When you call mysql_fetch_array it read its current row and move the pointer to next row. Same thing happening with your while loop. And also you need to remember it does not return the whole table. If it returned the whole table then you would get two dimensional array.
eng.bassem 8-May-13 1:33am
mmmmmmmmmmmmmmmmmmmm ,yes you are right it moves the pointer to next row . Thanks Mohibur , u are a helpfull man :)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In while loop
while( $data=mysql_fetch_array($result))
    echo $data[0].$data[1].$data[2].$data[3]

what it does? It just looping through the $result.So it give you the result as row1 row2 .....
But when you echo it with out the while loop it prints the first row .And echo it again it will give you the second row.

The idea in while loop is every time it fetches the row, adds it to array and echo for you automatically.

But without while the idea is you are manually echo every row.
@:- $data =mysql_fetch_array($result);

Using this line of code for the second time does not work as the result set is same.

If you change (while using second time)
$data =mysql_fetch_array($result); 

while( $data=mysql_fetch_array($result))
    echo $data[0].$data[1].$data[2].$data[3]

it will print from the second row to last row.

Tadit Dash 8-May-13 10:06am
Nice explanation. +5 ed.
eng.bassem 13-Jul-13 5:21am
Great , thank you so much .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100