why not simply having the username and the mail to be UNIQUE?
than you could do your insert
if mysql_insert_id returns something the entry has been inserted otherwise check mysql_error, it tells you something about "duplicate key" and also the column's name causing the duplicate key error
for example
CREATE TABLE user_login(
user_id int primary key auto_increment,
user_name varchar(50),
user_mail varchar(200),
UNIQUE KEY user_name (user_name),
UNIQUE KEY user_mail (user_mail)
);
and your php
$sql = "INSERT INTO users_login SET user_name = '".$thename."', user_mail = '".$themail."'";
mysql_query($sql);
if ($id = mysql_insert_id())
echo 'Entry successfully made.';
elseif (preg_match('%(#1062)(.*) (.*)$%si', mysql_error(), $matches))
if ($matches[3] == 2)
echo 'Error: Name already exists';
elseif ($matches[3] == 3)
echo 'Error: Mail already exists';
this does one INSERT only and not two selects and one insert and i therefor consider it to be have a better performance
the message for example is
#1062 - Duplicate entry 'nomail@localhost' for key 3
key 1 is the primary key