akrytus Posted March 12, 2008 Share Posted March 12, 2008 I have a MYSQL dbase and I am currently getting duplicate entries. Can someone give me some example code to check the MYSQL dbase if that record exists before I write it a second or third time? I know how to querry the dbase, but what do I do with that information? Quote Link to comment https://forums.phpfreaks.com/topic/95789-check-to-see-if-the-record-exists-before-writing-to-mysql/ Share on other sites More sharing options...
Lashiec Posted March 12, 2008 Share Posted March 12, 2008 You could take what you're entering, let's say it's a persons first name, and use that to check the database if there are any entries with the same first name. If there are, you wouldn't do the insert of the new information. Or, you could set the first name column to unique and then when you try to enter duplicate data mysql will reject the entry without you doing any checking. Quote Link to comment https://forums.phpfreaks.com/topic/95789-check-to-see-if-the-record-exists-before-writing-to-mysql/#findComment-490365 Share on other sites More sharing options...
haku Posted March 12, 2008 Share Posted March 12, 2008 Use the same data you are using for your insert statement to do a select statement, but instead of selecting any data, use "SELECT COUNT(*) FROM table WHERE field1=data1 AND field2=data2 LIMIT 1" This will return either 1 or zero. If it returns 1, then the database already contains the info. If it returns zero, then the data isn't int he database. so if it returns zero, insert the info into the database. Quote Link to comment https://forums.phpfreaks.com/topic/95789-check-to-see-if-the-record-exists-before-writing-to-mysql/#findComment-490370 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.