David19801
David19801

Reputation: 11448

mysql value not found

I have a mysql table and am using php. I have:

mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");

Now this returns an ID if email was found. However, I want to do something if it does not find $email in the email table column. How can I recognise when $email was not found and tell php?

Upvotes: 2

Views: 3200

Answers (4)

Joel Kennedy
Joel Kennedy

Reputation: 1611

All that is required to check if a result was not found is an if statement used with the mysql_num_rows function:

$check = mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");

$check2 = mysql_num_rows($check);

if ($check2 == 0) {
    // No email found, so show an error to the user
    echo "No email found!";
}
else
{
   // An email address was found, so do something with it here
}

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237847

mysql_query returns a result. You can call mysql_num_rows on that result to see the number of rows selected:

$result = mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
if (mysql_num_rows($result) > 0) {
    // found a result
} else {
    // no result found
}

Upvotes: 6

rik
rik

Reputation: 8612

If mysql_num_rows() returns 0, no row was selected because no email matched. http://docs.php.net/mysql_num_rows

Upvotes: 1

Jan
Jan

Reputation: 2293

I think that mysql_num_rows check should help.

Upvotes: 1

Related Questions