PHP - Error when I try to login - mysqli_num_rows () expects parameter 1 to be mysqli_result

2

Good afternoon, I was trying to study a registration system using session in php the part of the registry works normal, the problem is at the time of logging, even with the correct password I get the following 2 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 snippet of 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 in even though the information is 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
    
asked by anonymous 30.12.2017 / 21:47

3 answers

1

Basic typing error.

This line does not make 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=
         ^^^^^^^^^^^^
    
30.12.2017 / 22:23
2

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

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

 $ query = mysqli_query ($ conn, "SELECT * FROM login WHERE user = '" $ user. );

    
30.12.2017 / 22:25
0

Good afternoon friend, if I'm not mistaken this is missing one more parameter according to the error in the message.

I suggest you see in the php manual:

  

link

    
30.12.2017 / 22:07