Reputation: 45
Can anybody tell me why not all the results for MySQL aren't ending up in the array?
$result = mysql_query("select * from groups order by id desc");
if ($row = $result->fetch()) {
$groups[] = $row;
}
Upvotes: 1
Views: 44
Reputation: 8851
Because fetch only fetches a row as explained in the php manual:
Fetches the next row from a result set
I'd like to suggest changing your mysql_ code for PDO
$db = new PDO("..."); // Creates the PDO object. Put the right arguments for your connection.
$statement = $db->prepare("SELECT * FROM groups ORDER BY id DESC");
$statement->execute();
while ($groups = $statement->fetch())
{
// Do whatever you want to do
}
Upvotes: 0
Reputation: 4179
The code you have there does not iterate over the result set. Try this instead.
while ($row = $result->fetch()) {
$groups[] = $row;
}
Upvotes: 2
Reputation: 219804
Use while
not if
while ($row = $result->fetch()) {
$groups[] = $row;
}
Upvotes: 3