mysql query issues

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
Darkside33
Forum Newbie
Posts: 2
Joined: Sat Apr 02, 2005 11:00 am

mysql query issues

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_query() when running a SELECT, returns a resource handle. You have to use mysql_fetch_* to retrieve what it found.
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remember to quote your string constants. ;)
Darkside33
Forum Newbie
Posts: 2
Joined: Sat Apr 02, 2005 11:00 am

Post 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
Post Reply