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
https://forums.phpfreaks.com/topic/223989-phpmysql-determining-if-data-is-unique/
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);
		}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.