Please help me figure this out

Discussion in 'PHP' started by samir1, Jul 30, 2009.

  1. #1
    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);



    ?>
     
    samir1, Jul 30, 2009 IP
  2. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    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.
     
    Webray, Jul 30, 2009 IP
  3. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    UK Asian dating site? lol

    why do you have double paranthesis at the start of your while(( ???
     
    ezprint2008, Jul 31, 2009 IP
  4. samir1

    samir1 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    it did not work. Did not get any errors but nothing got deleted. Any thing else I can try
     
    samir1, Jul 31, 2009 IP
  5. PHP-Guru

    PHP-Guru Greenhorn

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #5
    i think there is a problem with quotations marks in the second version...
     
    PHP-Guru, Jul 31, 2009 IP
  6. samir1

    samir1 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    which quotation marks exactly are u referring to
     
    samir1, Jul 31, 2009 IP
  7. keaglez

    keaglez Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    keaglez, Jul 31, 2009 IP