MySQL problem... "not a valid result resource"

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

MySQL problem... "not a valid result resource"

Post by jkashu »

Code: Select all

// Get clicks
$clicks = mysql_query("SELECT
                      SUM(CASE WHEN clickdate = CURDATE() THEN 1 ELSE 0) AS 'numtoday',
                      SUM(CASE WHEN WEEK(clickdate) = WEEK(CURDATE()) THEN 1 ELSE 0) AS 'numthisweek',
                      SUM(CASE WHEN MONTH(clickdate) = MONTH(CURDATE()) THEN 1 ELSE 0) AS 'numthismonth',
                      SUM(CASE WHEN YEAR(clickdate) = YEAR(CURDATE()) THEN 1 ELSE 0) AS 'numthisyear'
                      FROM clicks WHERE affiliateID = '$affiliateID'");
 
// Check for errors
if(!$clicks){ mysql_error(); }
 
// Gets results from database
while($row  = mysql_fetch_array($clicks, MYSQL_ASSOC)){
 
    $clicksDay = $row['numtoday'];
    $clicksWeek = $row['numthisweek'];
    $clicksMonth = $row['numthismonth'];
    $clicksYear = $row['numthisyear'];
}   
   
This code tells me that $clicks is not a valid result resource? Thoughts?
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: MySQL problem... "not a valid result resource"

Post by nowaydown1 »

Looks fine to me just glancing at it. If you do a var_dump on $clicks what do you get?
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Re: MySQL problem... "not a valid result resource"

Post by jkashu »

The result of a var_dump of $clicks is:

bool(false)
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: MySQL problem... "not a valid result resource"

Post by nowaydown1 »

Well, if it's tossing you false then you can bet that some kind of SQL error is probably occurring. Not sure why your mysql_error() call isn't giving you any additional information though. I would toy around with it. Try to concat a string to your mysql_error() call so you can at least see if it's entering your conditional.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: MySQL problem... "not a valid result resource"

Post by John Cartwright »

mysql_error() returns the last error, it does not output it. You need to do something like,

Code: Select all

if(!$clicks){ die(mysql_error()); }
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: MySQL problem... "not a valid result resource"

Post by nowaydown1 »

Man. Can't believe I missed that. Been working with MDB2 too much ;)
Post Reply