homepage Welcome to WebmasterWorld Guest from 23.21.9.44
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Store MySql Query results in php array for later use
MySQl Array
beaudettee




msg:3427523
 3:15 am on Aug 21, 2007 (gmt 0)

Hi All:

I have a simple MySQL query that does a count to return values for 4 groups. I would like to store those values in a php array for later use but am a bit unsure of the syntax. I can get the values and write them out on the fly but am having difficulty storing them (and subsequently writing them ) after the connection is closed. here is my code snippet:

$Query = "SELECT qResultClass, COUNT(qTimeStamp) FROM quiz GROUP BY qResultClass";
$Result = mySql_db_query ($DBname, $Query, $Link);
while ($Row = mySql_fetch_array ($Result)) {
echo "There are ". $Row['COUNT(qTimeStamp)'] ." ". $Row['qResultClass'] ." items.";
echo "<br />";
}
mySql_close ($Link);

 

physics




msg:3427553
 4:39 am on Aug 21, 2007 (gmt 0)

Hi beaudettee and welcome to WebmasterWorld.com!
It depends on what you want to do but an example would be:

$values = array();
while ($Row = mySql_fetch_array ($Result)) {

$values[ $Row['COUNT(qTimeStamp)'] ] = $Row['qResultClass'];
}

foreach ( $values as $k => $v ) {
print ``$k is the key for $v<br />\n'';
}

print_r($values);


beaudettee




msg:3427747
 10:47 am on Aug 21, 2007 (gmt 0)

Thanks for the warm welcome and quick response physics.

I am getting closer! I plugged in your solution and for some reason I am missing the first result set. I kept my first echo statement in to compare values as follows:
$Query = "SELECT qResultClass, COUNT(qTimeStamp) FROM quiz GROUP BY qResultClass";
$Result = mySql_db_query ($DBname, $Query, $Link);
$values = array();
while ($Row = mySql_fetch_array ($Result)) {
echo "There are ". $Row['COUNT(qTimeStamp)'] ." ". $Row['qResultClass'] ." items.";
echo "<br />";
$values[ $Row['COUNT(qTimeStamp)'] ] = $Row['qResultClass'];
}
foreach ( $values as $k => $v ) {
print $k . "is the key for " . $v . "<br />\n";
}
mySql_close ($Link);
print_r($values);
****************************
and my result is:
****************************
There are 3 1 items.
There are 3 2 items.
There are 1 3 items.
There are 2 4 items.

3is the key for 2
1is the key for 3
2is the key for 4
Array ( [3] => 2 [1] => 3 [2] => 4 )

Any ideas? Thanks again for your help!

Eric B

beaudettee




msg:3428903
 2:31 pm on Aug 22, 2007 (gmt 0)

Ok.. So I figured it out. I had to switch my recordset columns around in your solution. That fixed it. Thanks again!

$Query = "SELECT qResultClass, COUNT(qTimeStamp) FROM quiz GROUP BY qResultClass";
$Result = mySql_db_query ($DBname, $Query, $Link);
$values = array();
while ($Row = mySql_fetch_array ($Result)) {
echo "There are ". $Row['COUNT(qTimeStamp)'] ." ". $Row['qResultClass'] ." items.";
echo "<br />";
$values[ $Row['qResultClass'] ] = $Row['COUNT(qTimeStamp)'];
}
foreach ( $values as $k => $v ) {
print $k . "is the key for " . $v . "<br />\n";
}
mySql_close ($Link);
print_r($values);

physics




msg:3430889
 4:55 am on Aug 24, 2007 (gmt 0)

Hey beaudettee, you're welcome and I'm glad you got the solution working :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved