I have writen this code but it is not working. Something is not write but I cant figure out what. The goal of this code is find the matched email address in the csv file compare it to the mysql profiles table and if a match is found delete the record. <?php $fp = fopen("dpreport.csv","r"); while ( ($line = fgetcsv($fp,1000) ) !== FALSE ){ $conn = mysql_connect("localhost","xxxx","xxxx"); mysql_select_db("ukasian_datingdb", $conn); $sql='SELECT id FROM Profiles WHERE Email = ' . $line . "'"; $result = mysql_query($sql); if ( $result ) { while($row = mysql_fetch_array($result)) { mysql_query('DELETE FROM Profiles WHERE Email= ' . $line . "'") or die(mysql_error()); } } mysql_close($conn); } fclose($fp); ?>
You only need to connect once and then do all you gotta do. I didn't test it, but did notice the connect statement. It was probably causing the problem. Try something like this: $conn = mysql_connect("localhost","xxxx","xxxx"); mysql_select_db("ukasian_datingdb", $conn); $fp = fopen("dpreport.csv","r"); while ( ($line = fgetcsv($fp,1000) ) !== FALSE ) { $sql='SELECT id FROM Profiles WHERE Email = ' . $line . "'"; $result = mysql_query($sql); if ( $result ) { while($row = mysql_fetch_array($result)) { mysql_query('DELETE FROM Profiles WHERE Email= ' . $line . "'") or die(mysql_error()); } } } close($fp); mysql_close($conn); PHP: I did this kinda quick and just rearranged things. Hope it works.
Try echo the $line and see if you get the correct string to compare... In addition, it usually better to trim the string before send it to query, such as: $line = trim($line); PHP: