P3t3r Posted January 1, 2008 Share Posted January 1, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/83931-solved-mysql-question/ Share on other sites More sharing options...
asmith Posted January 1, 2008 Share Posted January 1, 2008 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 !"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83931-solved-mysql-question/#findComment-427170 Share on other sites More sharing options...
Barand Posted January 1, 2008 Share Posted January 1, 2008 Add a UNIQUE KEY (user_id,user_ip) An attempt to add a dupe will then give an error making it impossible to have duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/83931-solved-mysql-question/#findComment-427211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.