Page 1 of 1

mysql query issues

Posted: Sat Apr 02, 2005 11:19 am
by Darkside33
Hi im relativly new to PHP and mysql and am working on my 1st public website i am curently working on a system to stop people from entering multiple comments on a page by redirecting them to another based on ip.

The ip code is:

Code: Select all

if (getenv('HTTP_X_FORWARDED_FOR')){$onlyip=getenv('HTTP_X_FORWARDED_FOR');} else {$onlyip=getenv('REMOTE_ADDR');}
The problem is with the my sql query:

Code: Select all

$ip = mysql_query("SELECT  ip FROM  `Table` WHERE ip LIKE '$onlyip' ");
ECHO $ip;
The problem when i echo $ip the page returns

Resource id #3

however if i enter an ip manualy as a string in place of $onlyip it will search the table for the coresponding ip as desired.

I would apreciate any help with this problem

Thanks for any help

Dav

Posted: Sat Apr 02, 2005 11:25 am
by feyd
mysql_query() when running a SELECT, returns a resource handle. You have to use mysql_fetch_* to retrieve what it found.

Posted: Sat Apr 02, 2005 12:37 pm
by cbrian
So, it would be like this:

Code: Select all

$ip = mysql_query("SELECT  ip FROM  `Table` WHERE ip LIKE '$onlyip' ");ECHO $ip = mysql_fetch_array($ip);
$ip = $ip[0];
That would give you want you want, or this.

Code: Select all

$ip = mysql_query("SELECT  ip FROM  `Table` WHERE ip LIKE '$onlyip' ");ECHO $ip = mysql_fetch_assoc($ip);
$ip = $ip[ip];
I'm sure there's other ways, also.

Posted: Sat Apr 02, 2005 12:42 pm
by feyd
remember to quote your string constants. ;)

Posted: Sat Apr 02, 2005 7:52 pm
by Darkside33
thank you guys your help is most apreciated as it has ended 3 days of torure on my part trying to fond a solution to what seemed a simple enough problem