1

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; 
}
2

3 Answers 3

3

Use while not if

while ($row = $result->fetch()) {
  $groups[] = $row; 
}
Sign up to request clarification or add additional context in comments.

Comments

2

The code you have there does not iterate over the result set. Try this instead.

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

Comments

0

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

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.