Mysql_fetch_array does not retrieve all rows

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

It returns

 2063:7 

I haven't used count before, so I'm not 100% sure apart from the columns of the table. It's late and I can go crazy.

Here is another example of what is 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>"; 

What outputs:

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

TL DR: I am sending requests that return multiple rows, but fetch_array gives me a small fraction of these rows in the resulting array.

+4
source share
3 answers

mysql_fetch_assoc returns only one row if you need to use a loop to extract all rows

 while($row = mysql_fetch_assoc($result)) { print_r($row); } 
+6
source

Each call to mysql_fetch_assoc($result); gives you one row of a result set:

( from the documentation )

mysql_fetch_assoc - select result string as associative array

Returns an associative array corresponding to the selected row, and moves the pointer of the internal data forward. mysql_fetch_assoc () is equivalent to calling mysql_fetch_array () with MYSQL_ASSOC for the optional second parameter. It returns only an associative array.

You should use the function in a loop:

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

An example in a document shows how this is done .

+7
source

mysql_fetch_assoc does not work, you need to call it several times to get all rows. Like this:

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

Source: https://habr.com/ru/post/1342609/


All Articles