Page 1 of 1

storing mysql query results in an array

Posted: Sun Dec 03, 2006 11:34 am
by gyszilagyi
Hi,

I'm trying to incorporate a chart class in my PHP script.

The chart class requires that series labels are defined like this:

Code: Select all

$graph->labels = "Jan.,Feb.,Mar.,Apr.,May";
or like this:

Code: Select all

$graph->values = array("Jan.,Feb.,Mar.,Apr.,May");
In my case series labels should come from a database and I'm having trouble creating an array from the results of the query that I could use for this purpose.

I'm using the following code:

Code: Select all

while($row=mysql_fetch_array($results)) {

        $array_labels[] = array($row['labels']);
		
}
Then I try to use

Code: Select all

$graph->values = $array_labels;
However, instead of the label names the text "Array" appears.

What am I doing wrong?

Posted: Sun Dec 03, 2006 12:08 pm
by feyd
If you're simply trying to echo an array, PHP will output "Array" because there's nothing more for it to actually do since you didn't specify what part of the array you wanted.

Some functions that may be of interest are: explode() and implode()

Posted: Sun Dec 03, 2006 3:31 pm
by gyszilagyi
Thanks, implode sounds just like what I need but I could not make it work the way I wanted.

To be more specific, I don not want to echo an array.

I would like to replace this:

Code: Select all

$graph->labels = "Jan.,Feb.,Mar.,Apr.,May";
With this:

Code: Select all

$graph->labels = $labels;
where $labels is the result of a database query.

Posted: Sun Dec 03, 2006 3:52 pm
by gyszilagyi
I figured it out, there was a stupid mistake in the original code I posted, when I created the array from the query results by looping through the rows.

This

Code: Select all

$array_labels[] = array($row['labels']);
should of course be

Code: Select all

$array_labels[] = $row['labels'];
Sorry and thanks!