Domanda

Before you say it has been answered before, I have tried everything, literally.

I am trying to count the number of rows in a mysqli query with two WHERE clauses.

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

My problem is that when I try logging in with an invalid password and/or username (the combination doesn't exists in the table), $counter is still 1 and it fulfills the requirements for the first if statement.

I have tried counting the rows in many ways, none of which worked.

Some of the other attempts were:

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

and

$counter = mysqli_num_rows($result);

I'm just trying to come up with a way to log my users in successfully, and show error messages to invalid login attempts.

Thank you for taking your time to read through this and try to help. :)

È stato utile?

Soluzione

if ($counter = 1) {

This will always = 1, presume you mean

if ($counter == 1) {

Altri suggerimenti

You need to fetch the row from the result before you can set your counter.

Change

$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'];

To

$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'];

The following query works perfectly for me:

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

resturns 0 if no matching record is found else the returns the number of matching records.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top