mysql_fetch_array does not retrieve all rows

前端 未结 3 1934
北荒
北荒 2020-12-11 06:54
$query = \"SELECT * FROM table\";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . \":\" . count($all);
<         


        
相关标签:
3条回答
  • 2020-12-11 07:20

    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.

    0 讨论(0)
  • 2020-12-11 07:28

    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);
    }
    
    0 讨论(0)
  • 2020-12-11 07:35

    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);
    }
    
    0 讨论(0)
提交回复
热议问题