Hello,

I have a tracking program I built for one of my websites so I know which keywords are getting visitors. Anyway, the tracking program works, but it fills my error logs with this one error:

PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...

Anyway, the way the code works is it searches a specific database for a particular keyword and if the keyword exists it increments the visit number. But if the keyword doesn't exist it adds the keyword to the database and sets the visit number to 1.

Here's the code I'm having trouble with - it gives me the error message on this line:

$viewer_check_numrows = mysql_num_rows($viewer_check_result);

Here's a bigger portion of code so you can get an idea of what's going on.

mysql_connect ( $host, $username, $password)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$viewer_check_query = "SELECT * FROM $tblname WHERE keyword = '$keyWord'";
$viewer_check_result = mysql_query($viewer_check_query);
$viewer_check_numrows = mysql_num_rows($viewer_check_result);


    //If numrows is equal to zero, then the user is new
    if($viewer_check_numrows == 0){
        //Add the new entry
        $test = "new entry added";
        $viewer_new_query = "INSERT INTO $tblname (keyword, cpc, clicks) VALUES
        ('$keyWord', '$defaultCPC', '$defaultClicks')";
        $viewer_new_result = mysql_query($viewer_new_query);
    } 
    
    //If numrows is not equal to zero, then add a new hit
    if($viewer_check_numrows != 0){
        //Add the new count
        $checkCount = "SELECT clicks FROM $tblname WHERE keyword = '$keyWord'";
        $checkCountResult=mysql_query($checkCount);
        $row = mysql_fetch_array($checkCountResult);
        $currentClicks = $row["clicks"]; //Gets #of clicks
        $clickCount = $currentClicks + 1;
        $addCount = "UPDATE $tblname SET clicks = $clickCount WHERE keyword = '$keyWord'";
        $addCountResult=mysql_query($addCount);
    }

As I said, the code works. But whenever a keyword doesn't exist it adds the mysql_num_rows() error code into my error log, which can get annoying after a week or so because it can fill my error log quickly. Basically, i just need a way to prevent the mysql_num_rows() error when a row doesn't exist. I've tried looking online but there are too many mysql_num_row() errors from people with syntax typos to make it easy for me to find a solution so i was hoping someone here would know a way around this. Also, i'm fairly new to this so it the code looks sloppy, that's why.

Thanks.

Here's my understanding of what's happening:

The mysql_query function is returning false because there were no rows (see PHP manual for mysql_query return values). Then, the mysql_num_rows function is taking the value false , which is not a resource (what it's expecting), and therefore is throwing an error.

A simple thing to do would be this:

//same as before
$viewer_check_result = mysql_query($viewer_check_query);

//initialize this
$viewer_check_numrows = 0;

//anything not falsy
if($viewer_check_result) {

	//assign a new value only in the case where there is a result
	$viewer_check_numrows = mysql_num_rows($viewer_check_result);
}

Here's a reference for what PHP considers "falsy" in the if context.

are you sure the mysql_query returns if there is no result?

if the query has syntax error, or SQL query is empty.

lets use mysql_error() , see the manual its easy to use.

"mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. "

Lets change the counter select command:

$sql="select count(id) FROM $table where `keyword`='$keyword'";
$result=mysql_query($sql) or die(mysql_error())

then get this number:

$nums=mysql_result($result, 0);
This article has been dead for over six months. Start a new discussion instead.