Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.