php if statment with mysql table ?

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
Gemster
Forum Newbie
Posts: 13
Joined: Thu Jul 29, 2010 10:38 am

php if statment with mysql table ?

Post by Gemster »

Hi, in my code i what a section of blacklisted ips. I decided to write these ips to a mysql database but having problems reading them.

There are severalk ip's in the database table name IP and just 1 colum(row) called ip.

anyways i have made this code:

Code: Select all

 mysql_connect("myhost", "myuser", "mypass") or die(mysql_error()); 
 mysql_select_db("mydbname") or die(mysql_error()); 


 $data = mysql_query("SELECT * FROM IP") 
 or die(mysql_error());
 
 $bip = mysql_fetch_array( $data );


if($host == ".$bip['ip'] . ") { die("<hr>This ip is blacklisted..."); }
its this part " if($host == ".$bip['ip'] . ") { die("<hr>This ip is blacklisted..."); } " that is throwing an error, i just need it to check $host agaist the mysql table to check if $host is one of the ip's blacklisted.

Thanks
Gemster
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php if statment with mysql table ?

Post by Jonah Bron »

That error gives you critical information, you should look at it and see what it says.

Using mysql_fetch_assoc() instead, it will probably fix the problem. It's more consistent anyway.

http://php.net/mysql-fetch-assoc
Gemster
Forum Newbie
Posts: 13
Joined: Thu Jul 29, 2010 10:38 am

Re: php if statment with mysql table ?

Post by Gemster »

Jonah Bron wrote:That error gives you critical information, you should look at it and see what it says.

Try using mysql_fetch_assoc() instead.

http://php.net/mysql-fetch-assoc
Ya i have googled and looked at them all, tryed loads of different metheds but still it fails :/

Here is the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in

The problem is with the if statement.

Thanks
Gemster
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php if statment with mysql table ?

Post by Jonah Bron »

Okay, I looked at the if statement. Do this instead:

Code: Select all

if($host == $bip['ip'])
It's very important that you understand strings thoroughly:

http://php.net/string
Gemster
Forum Newbie
Posts: 13
Joined: Thu Jul 29, 2010 10:38 am

Re: php if statment with mysql table ?

Post by Gemster »

Jonah Bron wrote:Okay, I looked at the if statement. Do this instead:

Code: Select all

if($host == $bip['ip'])
It's very important that you understand strings thoroughly:

http://php.net/string
Ok this works kinda, I have around 6 ips in the table but it is just working on 1 ip not all ips ? :/

Thanks
Gemster

EDIT: nvm, i got it working with this " while($bip = mysql_fetch_array( $data )) { code here } " .
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php if statment with mysql table ?

Post by Jonah Bron »

Oh, pff, duh. I wasn't thinking. Use this:

Code: Select all

 mysql_connect("myhost", "myuser", "mypass") or die(mysql_error());
 mysql_select_db("mydbname") or die(mysql_error());


 $data = mysql_query("SELECT * FROM IP WHERE ip == '" . mysql_real_escape_string($host) . "'")
 or die(mysql_error());

if (mysql_num_rows($data) > 0) { die("<hr>This ip is blacklisted..."); }
You should clean old IP addresses out periodically, because you don't want to permanently block a dynamic IP address.
Post Reply