使用PHP或SQL进行错误检查

Give the following table:

CREATE TABLE User (
Email VARCHAR(256) PRIMARY KEY,
Name VARCHAR(256),
);

I am trying to insert date into the table. To check for duplication, should I use SQL to select email from user where email = $email and check the number or rows return is 1 and if it is 1, I just use php to print error message OR Should I just try to insert the data into table and use the following to print error?

mysql_query('INSERT INTO ...');
if (mysql_errno() == 1062) {
    print 'no way!';
}

Which is a better way?

You can go for a query like this :

$sql = "INSERT INTO `table` VALUES ('$email','$Name')"
      ." WHERE NOT EXISTS (SELECT * FROM `table` WHERE `email`='$email')";

mysql_query($sql) or die("There's a duplicate.");`

Generally it's better to let the DBMS do the checking, because the functionality is already there and tested. You just need to handle the error messages.

If you insist on using your own code to do the checking, be prepared for many hours of brainstorming (given the complexity of the problem solved).