6
$query = "SELECT * FROM table";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . ":" . count($all);

This returns

2063:7

I have not used count before, so I'm not 100% sure it's not counting the table columns. It's late and I might be going nuts.

Here's another example of what's happening:

$result = mysql_query($query, $db);
echo "Rows: " . mysql_num_rows($result) . " <BR />";

$player_array = mysql_fetch_assoc($result);
echo "<pre>";
print_r($player_array);
echo "</pre>";

Which outputs:

Rows: 9 
Array
(
    [playerID] => 10000030
)

TL;DR: I submit queries which return multiple rows, but fetch_array only gives me a small portion of those rows in the resulting array.

3 Answers 3

8

mysql_fetch_assoc returns only one row in once you have to use loop to retrieve all rows

while($row = mysql_fetch_assoc($result))
{
   print_r($row);
}
Sign up to request clarification or add additional context in comments.

Comments

7

Every call to mysql_fetch_assoc($result); gives you one row of the result set:

(from the documentation)

mysql_fetch_assoc — Fetch a result row as an associative array

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.

You have to use the function in a loop:

$all = array();

while(($row = mysql_fetch_assoc($result))) {
    $all[] = $row;
}

The example in the document shows how it is done.

2 Comments

My apologies for being a dunce. This is on the tail end of a huge debug and I haven't used PHP much in years. That's my story!
@sejje: No need to apologise :)
2

mysql_fetch_assoc doesn't work that way, you need to call it multiple times to get all rows. Like this:

while ($row = mysql_fetch_assoc($db_result))
{
  print_r($row);
}

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.