Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
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
$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
$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 Smile | :)
Posted 7-May-13 18:05pm
Hercal689
Comments
Mohibur Rashid at 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 at 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
good
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); 
to
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
  Permalink  
v2
Comments
Tadit Dash at 8-May-13 10:06am
   
Nice explanation. +5 ed.
eng.bassem at 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
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 132
2 ProgramFOX 130
3 Maciej Los 115
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
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