Jump to content

Check to see if the record exists before writing to MYSQL


akrytus

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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.