How to count something in the field? mysql related

Discussion in 'PHP' started by x0x, Jun 23, 2009.

  1. #1
    I'll try to explain what I want to do.

    Basically, some emails in my database have more than 100 characters in them and it crashes my newsletter script. How can I find those emails in my database? table 'users' field 'email'.
     
    x0x, Jun 23, 2009 IP
  2. fdfandrade

    fdfandrade Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to check the length of the email.

    
    $sql = "SELECT email FROM users;";
    
    $result = mysql_query($sql) or die(mysql_error());
    
    while ($line = mysql_fetch_array($result)){
      if (strlen($line[0]) < 100)
        /* send email */
    }
    mysql_close($dbh);
    
    PHP:
    Cheers :)
     
    fdfandrade, Jun 23, 2009 IP
  3. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can use the mysql CHAR_LENGTH function
    example:
    SELECT email
    FROM `tablename`
    WHERE CHAR_LENGTH( `email` ) < 100

    This way you get only the emails with emails shorter than 100 chars/
     
    zeronese, Jun 23, 2009 IP
  4. fdfandrade

    fdfandrade Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @zeronese, using the CHAR_LENGTH is a better solution in fact, I didn't know this function :)
     
    fdfandrade, Jun 23, 2009 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Thanks guys!
     
    x0x, Jun 25, 2009 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Got another question. How to find emails that have some kind of special characters in them that could be crashing my script?
     
    x0x, Jul 14, 2009 IP