Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dears

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 .

PHP
<?php
$con=mysql_connect("localhost","root","")
if (!$con)
{
  die ("error ".mysql_error());
}

$query="select * from emp";

$result=mysql_query($query);

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

PHP
<?php
$con=mysql_connect("localhost","root","");
$query="select * from emp ";
$result=mysql_query($query);
$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
Comments
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.
Hercal 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

In while loop
PHP
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.
PHP
@:- $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)
PHP
$data =mysql_fetch_array($result); 

to
PHP
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.

Thanks
Debasis
 
Share this answer
 
v2
Comments
Nice explanation. +5 ed.
Hercal 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900