![]() |
| |||
| 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 |
| |||
| 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. |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
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 |