Kahuki Webmaster Forum and Discussion Community  

Go Back   Kahuki Webmaster Forum and Discussion Community > Website Development & Management > Programming > PHP



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-27-2006, 02:22 AM
Member
 
Join Date: Sep 2005
Posts: 72
diggme is on a distinguished road
MySQL Query SUM() function (in PHP)?

Hi, my problem is when I try to run the MySQL SUM() function through PHP it just returns a completely blank array. Here is my PHP code (modified):
$var = mysql_query("SELECT SUM(uniques) FROM stats WHERE unqiues BETWEEN '1000' AND '2000'");

I have checked the table and table field names and they are completely correct, and multiple 'uniques' values are between 1000 and 2000. With SUM() it shouldnt be returning an Array at all, just one value. Its complicated what I'm trying to do but I need all of the 'uniques' values added together whos values are between 1000 and 2000. I have the most updated versions of PHP and MySQL installed on my server, just updated a few weeks ago and this is my first problem. Thanks.
naa my not only will that not work its complete invalid non-sense if u know anything about PHP/MySQL

Reply With Quote
  #2 (permalink)  
Old 07-27-2006, 02:31 AM
Rookie
 
Join Date: Jan 2006
Posts: 8
rdpioneer is on a distinguished road
how about removing the parens?

$var = mysql_query "SELECT SUM(uniques) FROM stats WHERE unqiues BETWEEN '1000' AND '2000'";

Reply With Quote
  #3 (permalink)  
Old 07-27-2006, 02:36 PM
Rookie
 
Join Date: Oct 2006
Posts: 8
chuck666 is on a distinguished road
"$var = mysql_query("SELECT SUM(uniques) FROM stats WHERE unqiues BETWEEN '1000' AND '2000'");"

You've misspelled 'uniques' in your WHERE clause.

Reply With Quote
  #4 (permalink)  
Old 07-28-2006, 08:15 AM
Rookie
 
Join Date: Feb 2006
Posts: 5
pingu256 is on a distinguished road
The mysql_query() function returns a result handle (a reference to a list of results that then need retrieved), not a single value.

To use your example (SQL statement changed to name the resulting field):
$handle = mysql_query("SELECT SUM(uniques) AS myvalue FROM stats WHERE uniques BETWEEN '1000' AND '2000'");
// Check to see if we did something wrong
if( !$handle ) {
die( 'Error running query: ' . mysql_errno( $handle ) . ' ' . mysql_error( $handle ) );
}
// Fetch the row
$row = mysql_fetch_assoc( $handle );
// Grab the data we want out of the row
$myvalue = $row['myvalue'];

It may seem overly complicated, but it's very rare that you want just one single value out of a database. The functions are designed for retrieving row after row of data, each row containing multiple columns.

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Update MYSQL database table based on mysql query array? blackfox PHP 2 03-30-2007 02:11 AM
MySQL Query - use every table except this, this and this!? Shnurok PHP 2 03-16-2007 05:38 PM
MySQL how do I Query 2 tables? Dlachm1 PHP 1 12-04-2006 09:56 PM
ASP.Net and mysql realted query? DOSGuy Programming 2 11-16-2006 12:31 PM
MYSQL query - multiple COUNT in one query? Tobi PHP 2 09-06-2006 02:22 PM


All times are GMT. The time now is 09:45 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0