Jump to content

[SOLVED] MySQL question


P3t3r

Recommended Posts

How should I make up my table if I want to do the following most efficiently?

 

The table has 2 columns, e.g. "user_id" and "user_ip". At any login, a row (user_id,user_ip) is added, BUT no duplicate rows should be added.

 

E.g. a user can log in from many different IP's over time, and and many users can log in from the same IP over time, but for each pair (user_id,user_ip) there should only be one row. Otherwise the database will get overloaded.

 

I'm looking for a solution as efficient solution, i.e. one that doesn't slow logins too much.

 

Thanks in advance and happy new year!

Link to comment
https://forums.phpfreaks.com/topic/83931-solved-mysql-question/
Share on other sites

try this :

 

 

<?php


$connection = mysql_connect("localhost","adminusername","adminpassword")
$sql = "SELECT * FROM table_name WHERE user_id='$theuserid' AND user_ip='$theuserip'";
$result = mysql_query($sql,$connection);
$num = mysql_num_rows($result);

if ($num == 0)
     {
      $sql = "insert into table_name(user_id,user_ip) values ('$theuserid','$theuserip')";
      $result = mysql_query($result,$connection);
     }
else
     {
      //do nothing , OR 
      echo "duplicated user and ip !";
     }
?>

Link to comment
https://forums.phpfreaks.com/topic/83931-solved-mysql-question/#findComment-427170
Share on other sites

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.