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.