0

在你说它之前已经回答之前,我已经尝试了一切,从字面上看。

我正在尝试使用两个 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);

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

感谢您抽出宝贵时间阅读本文并尝试提供帮助。:)

4

3 回答 3

1
if ($counter = 1) {

这将始终 = 1,假设您的意思是

if ($counter == 1) {
于 2013-08-10T11:42:29.180 回答
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'];
于 2013-08-10T11:42:40.970 回答
0

以下查询非常适合我:

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

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

于 2013-08-10T11:47:17.653 回答