abinidi Posted August 21, 2008 Posted August 21, 2008 I'm trying to get MySQL to return a SUM of items in one of the columns in my database table, and convert that into a format that I can display in PHP. Here is the code I'm using on my .php page: >$queryDinnerNumber="SELECT SUM(dinner_number) FROM data WHERE dinner = 'yes'"; This works, because when I run the code on the SQL tab of PHPMYADMIN, I get a table that has one row and one column, with the resulting value in that cell. However, how do I display that value on my webpage? Here is the code I'm trying: ><? $queryDinnerNumber="SELECT SUM(dinner_number) FROM data WHERE dinner = 'yes'"; $resultDinnerNumber=mysql_query($queryDinnerNumber); echo $resultDinnerNumber; ?> people have been invited to the dinner. However, on my page, I see the following: Resource id #7 people have been invited to the dinner. How do I convert the value from the results into something I can display on my web page? Quote
OJB Posted August 21, 2008 Posted August 21, 2008 what you have returned is a resource, not actual data you need to do something like this: >$queryDinnerNumber="SELECT SUM(dinner_number) as total FROM data WHERE dinner = 'yes'"; $resultDinnerNumber=mysql_query($queryDinnerNumber); while($row = mysql_fetch_array($resultDinnerNumber)) { $actual_results[] = $row; } print_r($actual_results); // this will print out your results array so you can see the values! echo $actual_results[0]['total'] .'people have been invited to the dinner.'; Quote
abinidi Posted August 21, 2008 Author Posted August 21, 2008 Thank you. That was the piece I've been missing!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.