PHP - Error when trying to log in - mysqli_num_rows() expects Parameter 1 to be mysqli_result

Asked

Viewed 1,555 times

2

Good afternoon guys, I was trying to study a registration system using Session in php the part of the registration works normal, the problem is time to log in, even with the correct password I get the 2 following errors:

Notice: Undefined variable: query in C:\wamp64\www\Login\Logar.php on line 30
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in
C:\wamp64\www\Login\Logar.php on line 30

Here is the relevant excerpt from the code:

    <?php
        if(isset($_POST["submit"])){
            if(!empty($_POST['user']) && !empty($_POST['pass'])){
                $user = $_POST['user'];
                $pass = $_POST['pass'];
                $conn = new mysqli('localhost', 'root', 'admin') or die (mysqli_error());
                $db = mysqli_select_db($conn, 'login') or die("databse error");
                $query = mysqli_select_db($conn, "SELECT * FROM login WHERE user= '".$user."' AND pass='".$pass."'");
 linha 31 >>>   $numrows = mysqli_num_rows($query);
                if($numrows !=0)
                {
                    while($row = mysqli_fetch_assoc($query))
                    {
                        $dbusername=$row['user'];
                        $dbpassword=$row['pass'];
                    }
                if($user == $dbusername && $pass == $dbpassword)
                {
                session_start();
                $_SESSION['sess_user']=$user;
                //Redirect Browser
                header("Location:welcome.php");
                    }
                }
                    else
                    {
                    echo "Invalid Username or Password!";
                    }
                   }
                    else
                    {
                echo "Required All fields!";
                }
               }
    ?>

Every time I try to log even the information being correct I get the following error

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given in C:\wamp64\www\Login\Logar.php on line 31

3 answers

3

Well, I already found the error, sorry for the time, it was lack of attention, I was selecting the DB twice instead of running a query.

WRONG: $query = mysqli_select_db($Conn, "SELECT * FROM login WHERE user= '". $user." ' AND pass='". $pass."'");

CORRECT: $query = mysqli_query($Conn, "SELECT * FROM login WHERE user= '". $user." ' AND pass='". $pass."'");

2


Basic error of typing.

This line makes no sense:

$query = mysqli_select_db($conn, "SELECT * FROM login WHERE user=...

The function to execute a query is this:

$query = mysqli_query($conn, "SELECT * FROM login WHERE user=
         ^^^^^^^^^^^^
  • 1

    yes yes, that’s right buddy, thank you.

0

  • Thank you for answering friend, but I do not believe this possibility, for the following remarks: 1-$row_cnt = mysqli_num_rows($result); < described in the manual 2- The registration still 1 parameter as argument and does not point errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.