Birdun
Birdun

Reputation: 7

Fixing the error: mysql_num_rows(): supplied argument is not a valid MySQL result resource

I'm not sure why this is not working:

if($strFilterByStatus != 'all' &&  $strFilterByYear == 'all' && $strFilterByMonth !='all')
{
    $strSQL = "SELECT * FROM tblcase where recovered = '".$strFilterByStatus."' and monthreported = '".$strFilterByMonth."'" ;
}
else if($strFilterByStatus != 'all' &&  $strFilterByYear != 'all' && $strFilterByMonth !='all')
{
    $strSQL = "SELECT * FROM tblcase where recovered = '".$strFilterByStatus."' and yearreported = '".$strFilterByYear."' and monthreported = '".$strFilterByMonth."'" ;
}


if (mysql_num_rows($strSQL)==0)
{
    $strCaseExist = false;
}
else
{
    $SQL=mysql_query($strSQL);  
    $strCaseExist = true;
}

I have 2 different SQL statements and I just want to know if it will return a record or not.

Upvotes: 0

Views: 56

Answers (3)

JohnKochJR
JohnKochJR

Reputation: 133

you have to execute the query BEFORE checking for num rows.

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80629

mysql_num_rows requires a resultset to be passed not a string variable. Change to the following code:

$result = mysql_query( $strSQL );
$strCaseExist = (mysql_num_rows($result) == 0) ? false : true;

Upvotes: 2

Sanoob
Sanoob

Reputation: 2474

You are not executing the query.check this manual for more details on mysql_num_rows() ,change your code like this

$resrce =mysql_query($strSQL)
if (mysql_num_rows($resrce)==0)

Beware about mysql functions, they are insecure and deprecated. Choose mysqli or pdo

Upvotes: 2

Related Questions