Unespected SQL error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Unespected SQL error

Post by mikusan »

Here is the code and the error i get...
Two things to keep in mind:
1 the code seemed to work for a while
2 it broke :)

Code: Select all

if (!empty($_SERVER['HTTP_REFERER']))
	{
		$SQL = "SELECT referrer,count FROM vc_referrer WHERE referrer='{$_SERVER['HTTP_REFERER']}'";
		if (mysql_query($SQL))	{

			$Data = mysql_fetch_array($SQL);
			$count = $Data['count'] + 1;
			mysql_query("UPDATE vc_referrer SET count='$count' WHERE referrer='{$_SERVER['HTTP_REFERER']}'");

		}
		else	{

		mysql_query("INSERT INTO vc_referrer ( referrer ) VALUES ( '{$_SERVER['HTTP_REFERER']}' )");

		}
	}
The error i get:
Evil SQL error wrote: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/index.vc on line 31
Line 31 is the "$Data = mysql_fetch_array($SQL);
" line...

thanks
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I'm not a MySQL'r, but I would expect you to need to do something like (please note function names/argument counts might be a little off)

Code: Select all

$SQL =....
$result= mysql_query($SQL);
if (mysql_numrows($result))
{
 ...
  $data = mysql_fetch_assoc($result);
...
It looks like your are confusing the string containing the textual query and the resource pointer to a database result.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Yes that indeed was the problem... i think what i wanted to do was this but somewhere along the lines i altered my line of thought

Code: Select all

if ($SQL = mysql_query($query))
{
//do stuff
}
<b>EDIT</B>
Nope after a while that gave me a similar error:
Evil Evil SQL error wrote: Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in
What's up with this?
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

if ($SQL = mysql_query($query)) 
&#123; 
//do stuff 
&#125;
you would then need to add:

Code: Select all

mysql_num_rows($SQL);
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

I don't follow you sorry...
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

post your code that you are currently using.
Post Reply