guyfromfl Posted January 10, 2011 Share Posted January 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223989-phpmysql-determining-if-data-is-unique/ Share on other sites More sharing options...
guyfromfl Posted January 10, 2011 Author Share Posted January 10, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/223989-phpmysql-determining-if-data-is-unique/#findComment-1157543 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.