0

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'); }
4

2 に答える 2

0

これを試して:

$result = mysqli_query($connection, "SELECT COUNT(`ID`) AS count FROM users WHERE username='$usernamelogin' AND `active`='1'");
$row = mysqli_fetch_assoc($result);
var_dump($row['count'] < 1);

編集: 一重引用符を逆引用符に変更しました。

于 2013-08-04T15:40:39.333 に答える
0

ID フィールドを一重引用符で囲んでいるため、文字列です。それらを削除するか、次のようにフィールド識別子の周りに ` マークを使用してみてください。

SELECT COUNT(`ID`) FROM users

また、SQL に挿入された変数を一重引用符で囲んでいるため、最終的な修正は次のようになります。

"SELECT COUNT(`ID`) FROM users WHERE username=$usernamelogin AND `active`='1'"
于 2013-08-04T15:47:42.187 に答える