Jump to content

How to check if result exists in mysql table


jdock1

Recommended Posts

Basically what im doing is trying to check if a new users ip exists in the database, if so theup.n it will prevent the account signup.

 

I already have a table with the ip lists, but I need to match them, to see if it already exists.

 

Surely theres a way to do this? I already did my research, so please, don't  " :rtfm:" me!

 

Thanks guys!!!!

$query = "SELECT count(*) as countof FROM iptablename WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";

 

Fetch the result. 

 

Really bad idea unless you don't care about stopping people who share an IP (NAT'd) from signing up.

$query = "SELECT count(*) as countof FROM iptablename WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";

 

Fetch the result. 

 

Really bad idea unless you don't care about stopping people who share an IP (NAT'd) from signing up.

Hmm im not sure I really understand the code. Would it work like this?

$query = "SELECT count(*) as countof FROM mytable WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";
mysql_query($link,$query);
if ($query) {
echo "Duplicate IP";
}

So my understanding is that query is looking for the same IP in the table already? If thats so, then my code is valid, if that query is true & it found a matching IP, it echos the error?

 

Also, I have been thinking about that. Do alot ISPs issue the same IP? How common is that?

& what do you mean by nat'd?

 

Sorry for all the questions! Thanks for your reply& advice!

This may be the longer way around than using Count in the query, but doing something like this always works for me.

$result = mysql_query("SELECT * FROM iptablename WHERE ip = '{$_SERVER['REMOTE_ADDR']}'", $link);
$num_rows = mysql_num_rows($result);

if($num_rows>"0")
{
// do not let them sign up
}

else
{
// display signup code
}

 

Your code needs to actually look at the result of the count(*) query.

 

$query = "SELECT count(*) as countof FROM mytable WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";
$result = mysql_query($link,$query);
if ($result) {
  $row = mysql_fetch_assoc($result);
  if ($row['countof'] > 0) {
     // don't allow signup
  } else {
    // do signup

  }
}

Also, I have been thinking about that. Do alot ISPs issue the same IP? How common is that?

& what do you mean by nat'd?

 

Well, colleges/universities, hotels, apartments, Tim Hortons, any place like this that has free internet or some form of networking (like in a college) is most likely going to have one IP. Or at least not a unique IP for every user. So, if one person from a college signs up on your website then nobody else in that college can.

 

Plus, this is a rather useless form of protection because anyone can just go behind a proxy and still sign up multiple accounts.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.