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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.