0

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

4
  • 3
    mysql_* Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Commented Jun 28, 2016 at 15:03
  • Did you check that query itself works? Are there any other errors? Commented Jun 28, 2016 at 15:04
  • 1
    The query works and I'm using php 5 Commented Jun 28, 2016 at 15:19
  • Any errors mysql_error() Commented Jun 28, 2016 at 15:22

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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'];
}

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.