BbsMentos
BbsMentos

Reputation: 15

Fetch_array not working correctly

I'm request a database to get the 5 last mesures of a script, the problem is I can't convert the query into an array which I need to display an Histogram

$query=mysql_query('Select Nombre from mesure_actifs order by Date desc limit 5');
$valeurs=mysql_fetch_array($query); 

I get this

enter image description here

Upvotes: 0

Views: 63

Answers (2)

Raj
Raj

Reputation: 32

If you only fetch one row use mysql_fetch_assoc instead mysql_fetch_array.

For fetching multiple rows Use,

    $query=mysql_query('Select Nombre from mesure_actifs order by Date desc limit 5'); 
    while($valeurs=mysql_fetch_array($query)){
       $var = $valeurs['Nombre'];
}

Upvotes: 0

Shadow
Shadow

Reputation: 34232

As php manual on mysql_fetch_array() indicates:

Fetch a result row as an associative array, a numeric array, or both

This means that a call to this function fetches a single row from the resultset. This means that

$valeurs=mysql_fetch_array($query); 

code fetches only the 1st row from your resultset. As the examples on the linked manual page indicate, you need to use a loop to get all data retrieved from your resultset. The user contributed notes below the manual section describe how to create an array out of the results, if you do not know how to create one in a loop.

But you should truly move away from the mysql_*() functions and use mysqli or PDO instead.

Upvotes: 2

Related Questions