KeithDert
KeithDert

Reputation: 45

MySQL results issue in php array

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

Answers (3)

Francisco Presencia
Francisco Presencia

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

Craig Taub
Craig Taub

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

John Conde
John Conde

Reputation: 219804

Use while not if

while ($row = $result->fetch()) {
  $groups[] = $row; 
}

Upvotes: 3

Related Questions