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!
@mysql_connect("localhost","username","password") or die "Failed to establish connection with MySQL server";
@mysql_select_db("database") or die "Failed to select a database.";
$sql = "SELECT ip FROM banned_ip";
$temp = mysql_query($sql);
while($result = mysql_fetch_array($temp))
{
if($result == $REMOTE_ADDR)
{
echo "You're banned from the system";
mysql_close();
mysql_free_result($result);
exit();
}
}
and I timed this script and also timed the following code:-
$sql = "SELECT ip FROM banned_ip WHERE ip = '$REMOTE_ADDR'";
$temp = mysql_query($sql);
if($result == $REMOTE_ADDR)
{
echo "You're banned from the system";
mysql_close();
mysql_free_result($result);
exit();
}
I have manage to work out that the bottom code takes less time to execute. But I did not have much banned IP addresses in the database. Will this change if there is a lot of banned IP addresses in the database?
The bottom script is definitely the best in terms of efficiency as mySQL is built to search through large lists very quickly.
The first search method will get much slower the more IP addresses you have in the table whereas the 2nd will not be affected too heavily when more IP addresses are added as they should get indexed (you did create an index on that field didn't you - either PRIMARY KEY or a standard INDEX).
For the most efficient searching, your table should look something like this: