PDA

View Full Version : Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource


bubbles19518
06-13-2007, 03:50 PM
So I'm trying to track keywords from My PPC campaign on yahoo. They add $_GET['OVRAW'] and $_GET['OVKEY'], which are the search term and they keyword your actually bidding on. I'm trying to throw these in a database but get:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/creditca/public_html/cashrewardstest.php on line 14

I've gotten this error before, and have usually been able to fix it but I can't figure out whats wrong now. Heres my code:


if(isset($_GET['OVKEY']))
{
$rawkeyword = $_GET['OVRAW'];
$rawkeyword = urldecode($rawkeyword);
$keyword = $_GET['OVKEY'];
$keyword = urldecode($keyword);

$link = mysql_connect('192.168.2.7','creditca_creditc','cred1tcard');
mysql_select_db("creditca_creditcard", $link) or die("An error occured while trying to establish a connection to the specified database");

$sql = "SELECT * FROM keywords WEHRE keyword = '$keyword'";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0)
{
$sql = "INSERT INTO keywords (`id` , `keyword` , `raw`, `count`) VALUES ('', '$keyword', '$rawkeyword','0')";
$result2 = mysql_query($sql);
}
$subid = $result['id'];
$sql = "UPDATE keywords SET count = count+1 WHERE id = '$subid' LIMIT 1";
$result3 = mysql_query($sql);
mysql_close($link);
}


Any help is greatly appreciated.

Fumigator
06-13-2007, 04:13 PM
Good programming rule of thumb: Hande your errors. The error you are getting is saying the function mysql_num_rows() is being passed something other than a valid Query result (they call it a resource). So your next job is to figure out why your variable $result isn't a valid query result. You do that by checking the result after you call mysql_query().


$result = mysql_query($sql);
if (!$result) {
die("I JUST GOT A BLEEDIN QUERY ERROR!!!!!<br />Query is: $sql<br />Error is: ".mysql_error());
}

bobleny
06-13-2007, 07:17 PM
Well, it would help to spell things correctly... ;)
$sql = "SELECT * FROM keywords WEHRE keyword = '$keyword'";
try
$sql = "SELECT * FROM keywords WHERE keyword = '$keyword'";

Note the "WEHRE"?
------------------------------

A small tip:

When something like that happens, it often helps to echo what it is.

Like this:
$sql = "SELECT * FROM keywords WEHRE keyword = '$keyword'";
echo "<br /><br />" . $sql . "<br /><br />";
die();
$result = mysql_query($sql);

It will print the $sql and then die. This allows you to see what exactly is being queried. In this case, it would print:
SELECT * FROM keywords WEHRE keyword = '$keyword'

It makes it easier to check in that way.

Good luck!

Edit:
One other thing, is your id column auto increment?
If it is, you need not mention it when inserting or updating a row. MySQL automatically does all of it for you.

If it isn't, it should be.