Gazz1982 Posted January 25, 2011 Share Posted January 25, 2011 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."; } Quote Link to comment https://forums.phpfreaks.com/topic/225582-mysql-to-array/ Share on other sites More sharing options...
rondog Posted January 25, 2011 Share Posted January 25, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164802 Share on other sites More sharing options...
Gazz1982 Posted January 25, 2011 Author Share Posted January 25, 2011 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)']); } Quote Link to comment https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164808 Share on other sites More sharing options...
rondog Posted January 25, 2011 Share Posted January 25, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164809 Share on other sites More sharing options...
Gazz1982 Posted January 25, 2011 Author Share Posted January 25, 2011 oh it works... I mmust have done something wrong, thank you Quote Link to comment https://forums.phpfreaks.com/topic/225582-mysql-to-array/#findComment-1164813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.