Page 1 of 1

php if statment with mysql table ?

Posted: Wed May 11, 2011 6:32 pm
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

Re: php if statment with mysql table ?

Posted: Wed May 11, 2011 6:52 pm
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

Re: php if statment with mysql table ?

Posted: Wed May 11, 2011 6:56 pm
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

Re: php if statment with mysql table ?

Posted: Wed May 11, 2011 7:13 pm
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

Re: php if statment with mysql table ?

Posted: Thu May 12, 2011 4:43 am
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 } " .

Re: php if statment with mysql table ?

Posted: Thu May 12, 2011 12:42 pm
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.