2

enter image description here

i just want to fetch the invalid email addresses from my database, i tried with the following query, but its not working

$sql=mysql_query("SELECT * FROM mytable WHERE email!='' and email NOT REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$'");

And the invalid Email is a.bcdefg-3@abccom

Ezhil
  • 996
  • 8
  • 13
  • What happens now, no results returned or an error? Can you provide a bad email you'd expect this to find? – chris85 Jun 02 '15 at 13:25
  • 3
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 02 '15 at 13:25
  • its returned no result and the invalid email is a.bcdefg-3@abccom – Ezhil Jun 02 '15 at 13:26
  • Maybe use something like that: [a-zA-Z0-9][a-zA-Z0-9\.\_\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9]+\.[a-zA-Z]{2,4})+ – ventaquil Jun 02 '15 at 13:37

2 Answers2

3

It looks like the Richard answer is correct however, it may not works given the collation used. Therefore, if you have a case sensitive collation, you may want to lowercase your field.

Try this query :

SELECT * FROM `mytable` WHERE `email` NOT REGEXP '^[[:alnum:]._%-\+]+@[[:alnum:].-]+[.][[:alnum:]]{2,4}$';    

I have updated the regex to use character classes instead of character range to avoid lower (or upper) case transformation.

Moreover, in some IDE, you may have to escape "." with two backslashes, therefore I use

[.]

instead of escaped dot.

I updated again to allow subdomains. Edited to allow +, thanks to @Charlie Brumbaugh comment.

glefait
  • 1,651
  • 1
  • 13
  • 11
0

Try this:

SELECT * FROM `mytable` WHERE `email` NOT REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';

Works for my.

Furthermore, do not use the MySQL driver as it is deprecated in PHP.

Richard
  • 2,840
  • 3
  • 25
  • 37
  • Is it working in phpMyAdmin/Adminer? Are you sure that there is an invalid entry? – Richard Jun 02 '15 at 14:06
  • its not working in phpMyAdmin and pls find the screenshot of the row – Ezhil Jun 02 '15 at 14:11
  • @Ezhil: Try the edited RegEx. You should validate the email address at input time and it should not be allowed to insert a invalid email address in database. This is not consistent. – Richard Jun 02 '15 at 14:14