Mysql Results in Multidimensional Array

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Mysql Results in Multidimensional Array

Post by Etherguy »

I am trying to output mysql results into a multidimensional array so I can do some charting with it. I can't seem to get the data in the format I need. Which is :

Code: Select all

<?php
$chart[ 'chart_data' ] = array ( array ( "", "Dan", "Gary", "Tom", "Al", "Pete" ), array ( "", 63, 40, 10, 1, 0 ) );
?>

Basically I have 2 fields... a name field and a numeric field. If I manually do the MD-Array it works like a champ. If I try to use data from mysql is dies or loops forever.

I have tried imploding the data, which works fine when I am not trying to create a MD-Array.

Any ideas?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

I think we'll have to see some code...
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

Here is what I have

Code: Select all

<?php
$result1=MYSQL_QUERY("SELECT DISTINCT Username,sum(AcctSessionTime)/60  as SesTime From radacct where Username != '' group by Username");

while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) {
$usr[] = $line[Username];
$ses[] = $line[SesTime];
}
$chart[ 'chart_data' ] = array (array("",$usr) ,array( "",$ses)) ;

?>
The result is a never ending loop.

Let me know if there is anything else I can provide.
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

nothing on this.. huh...
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

change this:

Code: Select all

<?php
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
$usr[] = $line[Username]; 
$ses[] = $line[SesTime]; 
} 
?>
to this:

Code: Select all

<?php
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
$usr[] = $line['Username']; 
$ses[] = $line['SesTime']; 
} 
?>
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

No go on that. Made no difference.. after the loop finally ends.. I see in my graph a title of Array... which tells me that it is not seeing the data, but rather the array it self. As I stated before I have tried imploding the array, and the data looks ok when echoed... but does not work when I try to make a multidimensional array out of it.

:(
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

try:

Code: Select all

<?php
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
print_r($line);
} 
?>
and see what that shows. Also try print_r($chart) once you fill it and check to see that its properly filled.
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

The

Code: Select all

print_r($chart['chart_data']);
worked like a champ. I was able to see what I was missing in my array that was causing the looping issue.

Thank you for your time.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

yep - http://www.php.net is your friend!
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

Post by Etherguy »

More then just a friend... more like a favorite uncle or something.

I am sure if php.net kept stats on visiting IP addresses mine would rank up there :D

Thanks again for the help!
Post Reply