Jump to content

mysql to array


Gazz1982

Recommended Posts

firstly i am quite rusty at php/sql so sorry in advance.

 

I have a php script to produce graphs with the GD library, alls working well, but I want to modify it so the data comes from my database.

 

so I connect to the database in the usuall way but the script uses an array

 

$values = array("23","32","35","57","12","3","36","54","32","15","43","24","30");

 

so I need mysql to out put in the same format

 

This is what I have so far, it prints out the data with some formating, thank you for any help you can offer

 

Gary

 

// Make a MySQL Connection

$query = "SELECT Question, COUNT(Answer) FROM question1 GROUP BY Question"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){

echo "There are ". $row['COUNT(Answer)'] ." ". $row['Question'] ." items.";
}

Link to comment
https://forums.phpfreaks.com/topic/225582-mysql-to-array/
Share on other sites

you would simply use array_push to store the value

 

<?php
// Make a MySQL Connection
$query = "SELECT Question, COUNT(Answer) FROM question1 GROUP BY Question";
$result = mysql_query($query) or die(mysql_error());
$values = array();

// Print out result
while($row = mysql_fetch_array($result)){

   echo "There are ". $row['COUNT(Answer)'] ." ". $row['Question'] ." items.";
   array_push($values,$row['COUNT(Answer)']);
}
// $values is now an array of data so continue with your script
?>

Link to comment
https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164802
Share on other sites

Hi,

sorry but that doesnt seem to solve my problem... I will rephrase:

 

I have $values=array("1","3","7")

 

I need to change it so it is something like this:

$values=array(MySQL_result)

 

Here is the simplified code

from above

// Make a MySQL Connection

$query = "SELECT Question, COUNT(Answer) FROM question1 GROUP BY Question";
$result = mysql_query($query) or die(mysql_error());
$values = array();

// result
while($row = mysql_fetch_array($result))
{   
array_push($values,$row['COUNT(Answer)']);
}

Link to comment
https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164808
Share on other sites

Not sure I am understanding. So your script requires $values to be an array of numbers, right?

 

So I made an empty array called $values.

 

The while loop will loop through every record found that matches your query. I array_push each record, assuming the 'Answer' column is a number, to the $values array. What is the problem?

Link to comment
https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164809
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.