-1

Sorry for a slight duplication of Q but I think the previous answers are now depreciated (I've seen that in PHP 5.3 you need you use MySQLi_fetch_array())

I'm using jpgraph and trying to pull my data from a MySQL database. I have the following code which pulls the figures I need and when I echo the result it spits out the answers one by one no problem but it won't then put it into an array.

I'm trying to replace a line which is

$ydata = array(1,2,3,4,5,6,7);


//query
$sql = "SELECT CONCAT( 'Week ', WEEK( TimeofCompletion ) , ' ', YEAR( TimeofCompletion ) ) AS Week, Count( * ) AS VolumeOfAnswers
FROM table01
GROUP BY WEEK( TimeofCompletion ) , YEAR( TimeofCompletion ) 
ORDER BY TimeofCompletion";

$ydata = array();

while($row = mysql_fetch_array($sql)){
// Create a temporary array
 $temp = array();

 $temp[] = "".$row['VolumeOfAnswers']."";
$ydata = '['. implode(', ', $temp) . ']';   
}

Any suggestions how I would get the output by row into an array?

Regards Maudise

1 Answer 1

0
 $temp[] = "".$row['VolumeOfAnswers'].""; //what this suppose to do?

i assume you want quotes around i would add single quote inside

 $temp[] = "'".$row['VolumeOfAnswers']."'";

otherwise you dont need double quotes at all so i would remove them. After that your array will hold the values from the database.

according to documentation mysql_fetch_array is deprecated and you should be using PDO or MySQLi. But if you still want to use it, i would suggest using mysql_fetch_assoc instead.

Now about the question:

 while($row = mysql_fetch_array($sql)){
  // Create a temporary array
  $temp = array();

  $temp[] = "".$row['VolumeOfAnswers']."";
  //why is this here? should be outside of while loop shouldnt it?
  $ydata = '['. implode(', ', $temp) . ']';   
 }

your $ydata always gets overwritten and holds only the last element of $temp array. Either move $ydata outside of while loop or use array_merge($ydata, $temp);

1
  • hi, for some reason it's telling me the array is empty? Other errors I've had are that it can't define a min/max value which means I think it's reading it as string variables rather than numerical. Any ideas? Commented Jan 22, 2013 at 23:25

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.