I'm using PHP to build a login form, and one part of the form validation is checking whether or not the user is "active," which is an integer in the mysql table users that will be equal to 0 or 1(1 being an activated). In my current setup, the user can't login if they don't have an active account. My first attempt to implement this was:
//This code won't work because the query is an object, not an integer.
elseif(mysqli_query($connection, "SELECT COUNT('ID') FROM users WHERE username='$usernamelogin' AND 'active'='1'") < 1) {
echo('Account not activated. ');
}
But that was not working, because the result is an object, not an int. So, I searched for how to solve this problem, and was given the following solution.
//if 1, account is active, if 0, account is inactive
$result = mysqli_query($connection, "SELECT COUNT('ID') FROM users WHERE username='$usernamelogin' AND 'active'='1'");
$row = mysqli_fetch_assoc($result);
$booly = ($row != 1) ? 1 : 0;
echo $booly;
This code, however, only gives an answer of 1, even when I would expect it to give a 0. Where have I gone wrong? What other methods could I use to implement this feature?
UPDATE:
I was indeed incorrectly using single quotes instead of backticks, so I tried implementing all of the following variations (I believe the first one to be correct, but tried all the others when it didn't work). None of them have changed my results.
"SELECT COUNT(`ID`) AS count FROM users WHERE username='$usernamelogin' AND 'active'='1'"
"SELECT COUNT(`ID`) FROM users WHERE username='$usernamelogin' AND active='1'")
"SELECT COUNT(`ID`) AS count FROM users WHERE 'username'='$usernamelogin' AND 'active'='1'"
"SELECT COUNT(`ID`) FROM users WHERE 'username'='$usernamelogin' AND 'active'='1'")
"SELECT COUNT(`ID`) AS `count` FROM `users` WHERE `username`='$usernamelogin' AND `active`='1'"
"SELECT COUNT(`ID`) FROM `users` WHERE `username`='$usernamelogin' AND `active`='1'")
I tried these as well, to no avail:
mysqli_query($connection, "SELECT COUNT(`ID`) FROM users WHERE username=$usernamelogin AND `active`='1'");
mysqli_query($connection, "SELECT COUNT(`ID`) FROM users WHERE username=$usernamelogin AND active='1'");
SOLUTION:
The solution implemented into the code:
$result = mysqli_query($connection, "SELECT COUNT(`ID`) AS count FROM users WHERE username='$usernamelogin' AND `active`='1'");
$row = mysqli_fetch_assoc($result);
if($row['count'] == true) {
echo('account not active'); }