Jump to content

how to check mysql for duplicates on insert


Looktrne

Recommended Posts

I have a script that writes emails to a database

 

I need it to check if that email already exists in the database

 

mysql_query("INSERT INTO brain (mail, ism) 
VALUES ('$ie', '1')")or die(mysql_error());  

 

how can I change this code to not inset if the value of mail already exists in the database

 

it also needs to be fast because this script is handling 1000 of inserts

 

thanks

 

Paul

Link to comment
Share on other sites

just do a SELECT * FROM [table][tr][td] WHERE column='VALUE'

 

then do an if statement to say wethor or not to do the query

 

if($THIS==$VALUE)

{

//THERE IS A DUPLICATE

}

else

{

//THERE ISN"T A DUPLICATE SO DO INSERT

 

mysql_query("INSERT INTO brain (mail, ism)

VALUES ('$ie', '1')")or die(mysql_error()); 

}

 

something like that

Link to comment
Share on other sites

If you do it that way, all the checking will be done in php and will definately take longer than comparing in the sql.

SELECT * FROM Table WHERE Column = '".$newValue

Where $newValue is a value that you are going to put into the db, but you want to check if it already exists. Then just see if it returned any results and if it did, don't insert it.

if (!mysql_num_rows($query))
  //Insert into

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.