Mysql_fetch_assoc retrieves only one record

I have an array with the result of the query:

.....some code..... $result = mysql_query("SELECT * FROM vttest"); $row = mysql_fetch_assoc($result); print_r($row); 

But it prints only one record, I can not put $ row in a loop, because I will work on sorting this variable later.

 Array ( [id] => 3 [rep] => Mike [name] => Joe [department] => Business [location] => Room 1 [email] => xxxx@hotmail.com [phone] => 519-123-4567 [type] => Visit [drink] => Coffee [notes] => Some notes [lastVisited] => 2010-08-27 [nextVisit] => 2010-08-27 ) 

I know there are more entries than I can print all of this while still being able to work with the $ row variable?

+4
source share
4 answers

In fact, you are getting one record because you need to use a loop to get all the records .:

 $my_arary = array(); while($row = mysql_fetch_assoc($result)){ $my_arary[] = $row; print_r($row); } 

As you said, later you can use $my_arary to do whatever you like.

+11
source
 $result = mysql_query("SELECT * FROM vttest"); $data = array(); while($row = mysql_fetch_assoc($result)){ array_push($data, $row); } 
+2
source

You need to put it in a loop. And then, if you need to work with this variable later, use mysql_data_Seek to reset the internal result pointer:

  mysql_data_seek($result, 0); // your later code. 
+1
source

You almost had it, you had to use a loop to get the results to get each row, and as you do, assign each row to the table.

The following is what you are looking for.

 $result = mysql_query("SELECT * FROM vttest"); while($row = mysql_fetch_assoc($result)){ $rows[] = $row; } 

Then, all you need to do to access this array is the following.

 foreach($rows as $index => $value){ print_r($value); } 

The preview will print each line in your array one at a time, allowing you to cut or use this data as you wish.

+1
source