Jump to content

PHP/MySQL determining if data is unique.


guyfromfl

Recommended Posts

I am trying to write a script that runs through a CSV file and inserts unique values in a mysql database and spits the duplicate out into a new CSV.

 

I can find the duplicates when I load it into an array, but when I insert them into a MySQL database, I cannot determine if they are duplicates. 

 

If I use INSERT IGNORE, it will run the whole file, and reject the duplicates.  This is fine, except I need to create a file of the duplicate entries.

 

What is the best way to determine if the insert attempt was rejected?

 

I could do a select to see if the row is there, spit it out...insert if not, but I was thinking there should be a way for MySQL to talk back to me to cut the queries in half...

 

Any ideas?

 

Thanks

Link to comment
Share on other sites

Basically I want something like:

 


$sql = "INSERT IGNORE INTO leads.prospect 
					(email, phone, ip, firstName, lastName)
				VALUES ('" . trim($data[3]) . "', 
						'" . $format->mysqlSafe($data[2]) . "',
						'" . $format->mysqlSafe($data[5]) . "', 
						'" . $format->mysqlSafe($data[0]) . "', 
						'" . $format->mysqlSafe($data[1]) . "')";

		$result = $db->query($sql);

                        // I want something like this:
		if ($result) {
			$added++;
		} else {
                                 // Add to the duplicate array, to be added to the new CSV file later
			$dup = array_push($data);
		}

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.