storing mysql query results in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gyszilagyi
Forum Newbie
Posts: 9
Joined: Sun Nov 19, 2006 12:48 pm
Location: Budapest, Hungary

storing mysql query results in an array

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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()
gyszilagyi
Forum Newbie
Posts: 9
Joined: Sun Nov 19, 2006 12:48 pm
Location: Budapest, Hungary

Post 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.
gyszilagyi
Forum Newbie
Posts: 9
Joined: Sun Nov 19, 2006 12:48 pm
Location: Budapest, Hungary

Post 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!
Post Reply