refiking Posted April 27, 2009 Share Posted April 27, 2009 I have a db field name "id" that is autoincrement and I want to run the query to get the most recent record that matches the query. What do I need to do to accomplish this? $sql = mysql_query("SELECT * FROM `phplist_user_user` WHERE `email` = '$email'")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/ Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 'most recent' implies using some kind of timestamp. You cannot rely on auto_increment to indicate the order in which record were inserted. If you want to take a risk though: SELECT * FROM `phplist_user_user` WHERE `email` = '$email' ORDER BY id DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820348 Share on other sites More sharing options...
refiking Posted April 27, 2009 Author Share Posted April 27, 2009 When I ran that, I got this error: Duplicate entry 'my@email.com' for key 2 Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820360 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 Hardly possible. It's not this query that generated this error. Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820365 Share on other sites More sharing options...
refiking Posted April 27, 2009 Author Share Posted April 27, 2009 You're right. It's this query: mysql_query("INSERT INTO `phplist_user_user` (`email`,`confirmed`,`htmlemail`,`paystatus`) VALUES ('$email', '1', '1','$id')")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820370 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 And it said you tried to insert 'my@email.com' into a column on which a UNIQUE index is created, where such entry already exists. Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820390 Share on other sites More sharing options...
refiking Posted April 27, 2009 Author Share Posted April 27, 2009 ahhh ok. So, should I change it from unique to something else? Or should it be deleted altogether? Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820400 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 I don't know. Do you want to allow non-unique e-mails in this column? Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820410 Share on other sites More sharing options...
refiking Posted April 27, 2009 Author Share Posted April 27, 2009 yes Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820411 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 So you shouldn't have UNIQUE index on this column. Quote Link to comment https://forums.phpfreaks.com/topic/155858-retrieve-last-duplicate-record/#findComment-820414 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.