Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_assoc fetching only one record

Tags:

php

mysql

I have an array with the result of a query:

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

But it only prints one record, I can't put $row in a loop because I will be working on sorting that variable later.

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

I know there are more records than that, how can I print all of it while still being able to work with the $row variable?

like image 345
Mankind1023 Avatar asked Aug 27 '10 19:08

Mankind1023


People also ask

What will mysql_fetch_assoc () return?

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

What does Mysqli_fetch_assoc () do in PHP?

The mysqli_fetch_assoc() function is used to return an associative array representing the next row in the result set for the result represented by the result parameter, where each key in the array represents the name of one of the result set's columns.

What is the difference between Fetch_assoc and Fetch_array?

fetch_array returns value with indexing. But Fetch_assoc just returns the the value. here array location 0 contains 11 also this location name is 'id'. means just returns the value.

What does Fetch_assoc mean?

Definition and Usage The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array. Note: Fieldnames returned from this function are case-sensitive.


1 Answers

You are actually getting one record because you need to use a loop to get all records.:

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

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

like image 112
Sarfraz Avatar answered Sep 28 '22 00:09

Sarfraz