Forum Moderators: coopster
However, when checking to see if the email they entered already exists I get a logical error by where the existing email variable is never equal to 0.
0 is what the variable would be at if there were no existing records.
Here's my code:
$existing_email = 0;
$result = mysql_query("SELECT COUNT(*) FROM user WHERE user_email = '$email_1'");
while($row = mysql_fetch_array($result))
{
$existing_email ++;
} Then below, during my validation, I have the following:
elseif($existing_email != 0)
{
header('Location: '. ERROR);
} Any help would be great because I'm stumped!
Regards,
Tom.
Edit:
I've used a different approach. Here's my result for people who are curious in future:
$result = mysql_query("SELECT user_email FROM user WHERE user_email = '$email_1'");
while($row = mysql_fetch_array($result))
{
$existing_email = $row['user_email'];
}
and then the error checking...
elseif($email_1 == $existing_email)
{
header('Location: '. ERROR);
}
[edited by: Tom_Cash at 2:01 am (utc) on Dec. 8, 2008]
SELECT COUNT(*) FROM user WHERE user_email = '$email_1' returns exactly one row of one item: the number of rows in your database for which user_email = '$email_1' is true. Thus, if the e-mail address is not in your database, it returns the single row
[ 0 ], and if the e-mail address is in your database, it returns the row [ 1 ]. Your while loop, meanwhile, was counting the number of rows returned--which is always one! For the COUNT(*) solution to work, you have to actually use the count returned.
while($row = mysql_fetch_array($result))
{
print_r($row); // add to see what $row looks like
$existing_email ++;
}
For the COUNT(*) solution to work, you have to actually use the count returned.
Exactly, pinterface is Correct. This Simple thing should do the trick
$result = mysql_query("SELECT COUNT(*) as existing_email_count FROM user WHERE user_email = '$email_1'");
$row = mysql_fetch_array($result);
$existing_email=$row['existing_email_count'];
Simple ;)
[edited by: Anyango at 4:31 am (utc) on Dec. 8, 2008]