doubi9255 2013-08-04 15:38
浏览 35
已采纳

PHP用户帐户激活检查不起作用

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'); }
  • 写回答

2条回答 默认 最新

  • dso0139 2013-08-04 15:40
    关注

    Try this:

    $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);
    

    Edit: Changed single quotes to back quotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路