Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search an array 1

Status
Not open for further replies.

mufka

ISP
Dec 18, 2000
587
US
Is there a way to use something like in_array() to see if a variable is in a MySQL array?

Something like (incomplete for simplicity):
Code:
$ip = @$REMOTE_ADDR;
$result = mysql_query("SELECT ip_address from banned_ip");
if (in_array($ip,$result)) {
     echo 'UNAUTHORIZED ACCESS';
     exit();
}

I have it working using a while loop, but I'm hoping there is a simpler way to do it.
 
Hi

What is "a MySQL array" ?

Anyway, [tt]mysql_query()[/tt] returns a resource, not an array.

And anyway, that is not the way to test such thing.
Code:
$ip = @$REMOTE_ADDR;
$result = mysql_query("select 1 from banned_ip where ip_address='".mysql_real_escape_string($ip)."'");
if (mysql_num_rows($result)) {
     echo 'UNAUTHORIZED ACCESS';
     exit();
}

Feherke.
 
Doh! I should have thought of that. Sometimes simple escapes me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top