计算有条件的行?

时间:2013-08-10 11:31:47

标签: php login counting login-script

在你说之前已经回答之前,我已经尝试了所有的事情。字面意思。

我试图用两个WHERE子句计算mysqli查询中的行数。

if (isset($_POST['member_name']) and isset($_POST['memeber_password'])) {
    $member_name_input = mysqli_real_escape_string($query, $_POST['member_name']);
    $member_password_input = mysqli_real_escape_string($query, $_POST['member_password']);

    $result = mysqli_query($query, "SELECT count(*) AS member_count FROM `members` WHERE `member_name` = '$member_name_input' AND `member_password` = '$member_password_input'") or die(mysqli_error($query));
    $counter = $row['member_count'];

    if ($counter = 1) {
        $result = mysqli_query($query, "SELECT * FROM `members` WHERE `member_name`='$member_name_input' AND `member_password`='$member_password_input'") or die(mysqli_error($query));
        $row = mysqli_fetch_array($result);
        $member_suspended = $row['member_suspended'];
        $member_validation = $row['member_validation'];
    }

    if ($member_suspended < time() and $member_validation = 2) {
        $_SESSION['member_id'] = $row['member_id'];
        echo $counter;
    } elseif ($member_suspended >= time()) {
        $suspension_date = date('d. m. y.', $member_suspended);
        echo '<div class="left_container"><h3>You are suspended until '.$suspension_date.'.</h3></div>';
    } elseif ($member_validation = 1) {
        echo '<div class="left_container"><h3>Your account has not been confirmed, yet.</h3></div>';
    } else {
        echo '<div class="left_container"><h3>Login unsuccessful.</h3></div>';
    }
} 

我的问题是,当我尝试使用无效的密码和/或用户名登录时(表中不存在该组合),$ counter仍然是1,它满足第一个if语句的要求。

我尝试过以多种方式对行进行计数,但都没有。

其他一些尝试是:

$counter = 0;
while ($row = mysqli_fetch_array($result)) {
++$counter;
}

$counter = mysqli_num_rows($result);

我只想尝试成功记录用户的方法,并向无效的登录尝试显示错误消息。

感谢您花时间阅读本文并尝试提供帮助。 :)

3 个答案:

答案 0 :(得分:1)

if ($counter = 1) {

这总是= 1,假设你的意思是

if ($counter == 1) {

答案 1 :(得分:0)

您需要先从结果中获取行,然后才能设置计数器。

更改

$result = mysqli_query($query, "SELECT count(*) AS member_count FROM `members` WHERE `member_name` = '$member_name_input' AND `member_password` = '$member_password_input'") or die(mysqli_error($query));
$counter = $row['member_count'];

$result = mysqli_query($query, "SELECT count(*) AS member_count FROM `members` WHERE `member_name` = '$member_name_input' AND `member_password` = '$member_password_input'") or die(mysqli_error($query));
$row = mysqli_fetch_assoc($result);
$counter = $row['member_count'];

答案 2 :(得分:0)

以下查询对我来说非常有用:

SELECT count(*)AS member_count FROM members WHERE member_name ='$ member_name_input'AND member_password =' $ member_password_input'

如果没有找到匹配的记录,则恢复0,否则返回匹配记录的数量。