CodeGuy
CodeGuy

Reputation: 23

PHP database query returns false everytime

I am building just a very simple login service in PHP, the database query is returning false every single time. I have added email addresses into the users table in my database using phpmyadmin, but when I enter one of the email addresses into the field and push submit, the query goes to the database and comes back false every time.

here is the code:

//queries the database and checks if the email address exists
$result = mysqli_query($connection, "SELECT * FROM 'users' WHERE 'Email'='$email'");
if ($result == FALSE){
die("The account for <i>$email</i> doesn't not exist!");
}

I know that the email variable from the form is correct because it gets printed out as an error. I also know that the email address in the database matches it exactly. The query however only returns false.

Thank you for your help.

Upvotes: 2

Views: 125

Answers (1)

Kevin
Kevin

Reputation: 41893

Just as @Jared has said in the comments you're using ' single quotes on your table and column names. You can convert them it into backticks or just remove them.

Backticks are required if your column, table, and database names are included in the reserved names of MySQL.

Since you're using mysqli_*. I suggest you use prepared statements and use num_rows instead. Example:

// use prepared statements
$stmt = $connection->prepare('SELECT * FROM `users` WHERE `Email` = ?');
//                                           ^ backticks   ^
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0){
    // found
} else {
    die("The account for <i>$email</i> doesn't not exist!");  
}

Upvotes: 2

Related Questions