Jump to content

Recommended Posts

Posted

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?

Posted

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.';

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...