-4

Here is what I have so far:

$checkbonus = mysql_query("Select theurl from `vaultpromo` where (accid=0 or accid=$acctype) and (shownum=$clickstoday or (showtype=1 and ($clickstoday%shownum=0))) AND 
(startdate<$today AND enddate>$daytime) OR (startdate=$today AND starttime<=$currenttime AND enddate>$daytime) limit 1");

if (mysql_num_rows($checkbonus) > 0) {
    DO SOMETHING
}

But I am getting this error:

Warning: mysql_num_rows() expects parameter 1 to be resource,
boolean given in...

Any help would be greatly appreciated!

Everything works until this part:

(startdate<$today AND enddate>$daytime) OR (startdate=$today AND starttime<=$currenttime AND enddate>$daytime) limit 1");

I have checked and it is checking for correct rows in the table and I have defined $today,$daytime, and $currenttime just prior to the query with this:

$today=date("Y-m-d");

$currenttime=date("H:i:s");

$daytime=date("Y-m-d H:i:s");
  • 2
    Format your code PLEASE! Don't rely on the community to format code for you! – Cranio Jun 16 '12 at 14:40
  • You are closing too many paranthesis – phpmeh Jun 16 '12 at 14:43
  • 2
    Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Jun 16 '12 at 14:46
  • 2
    @phpmeh I don't think so, parentheses seem ok to me. – Cranio Jun 16 '12 at 14:47
  • 1
    I tend to write the query line as two separate lines: `$sql = ' ... ';` and then `mysql_query($sql)`. It is easy then to echo out the query, to see if it is correct. – halfer Jun 16 '12 at 14:57
  • Fair point from @halfer, always better to separate queries from calls. It helps you to debug better, expecially in cases like this – Cranio Jun 16 '12 at 15:41

2 Answers2

2

The reason is that your query is not valid. So it returns False, a boolean, instead of a resultset.

mysql_num_rows() doesn't expect a boolean but a resultset, so it issues the warning.

Check if all the PHP variables are valued or someone of them is empty.

Edit: for example, if $acctype is empty, the query becomes like

Select theurl from `vaultpromo` where (accid=0 or accid=) and (...
                                                    ^^^^^^

Please ensure that every variable has a value, check to avoid strange situations...

Cranio
  • 9,380
  • 2
  • 33
  • 54
0

You should do something like this:

$checkbonus = mysql_query("Select theurl from `vaultpromo` where (accid=0 or 
accid=$acctype) and (shownum=$clickstoday or (showtype=1 and                 
($clickstoday%shownum=0))) AND 
(startdate<$today AND enddate>$daytime) OR (startdate=$today AND 
starttime<=$currenttime AND enddate>$daytime) limit 1");


//Check resultset variable if it contain data or not.
if($checkBonus)
{
    if (mysql_num_rows($checkbonus) > 0) {
       //DO SOMETHING
    }
}
else
{
    die('Invalid query: ' . mysql_error());
}

You are getting this error "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in..." because the variable $checkbonus does not contain the the Database resource and False is being returned, because your query did run successfully.

Your query most likely contains an error because one of more of the variables was either empty or not escaped so causing an error in the query.

Btw, I would strongly suggest that you look into using PDO library.

Haroon
  • 1,087
  • 1
  • 11
  • 13