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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With