Login form problems mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean [duplicate]

191
December 03, 2017, at 7:36 PM

This question already has an answer here:

  • mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row() expects parameter 1 to be resource 33 answers

I am trying to get create a login page and getting this error mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean

on this line when

<?php
$error = ''; //Variable to Store error message;
if ( isset( $_POST[ 'submit' ] ) ) {
    if ( empty( $_POST[ 'user' ] ) || empty( $_POST[ 'pass' ] ) ) {
        $error = "Username or Password is Invalid";
    } else {
        //Define $user and $pass
        $user = $_POST[ 'user' ];
        $pass = $_POST[ 'pass' ];
        //Establishing Connection with server by passing server_name, user_id and pass as a patameter
        $conn = mysqli_connect( "localhost", "root", "" );
        //Selecting Database
        $db = mysqli_select_db( $conn, "Bookswap" );
        //sql query to fetch information of registerd user and finds user match.
        $query = mysqli_query( $conn, "SELECT * FROM userpass WHERE pass='$pass' AND user='$user'" );
        $rows = mysqli_num_rows( $query );
        if ( $rows == 1 ) {
            header( "Location: welcome.php" ); // Redirecting to other page
        } else {
            $error = "Username of Password is Invalid";
        }
        mysqli_close( $conn ); // Closing connection
    }
}
?>
Answer 1

Don't give the space inside POST, You have to check the connection here

https://www.w3schools.com/php/php_mysql_connect.asp

If you are using the password then you have to use the password_hash(http://php.net/manual/en/function.password-hash.php) and password_verify(http://php.net/manual/en/function.password-verify.php) for more secure. Also I added the sql injection like $conn->real_escape_string

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$error = ''; //Variable to Store error message;
if ( isset( $_POST['submit'] ) ) {
    if ( empty($_POST['user'] ) || empty($_POST['pass'] ) ) {
        $error = "Username or Password is Invalid";
    } else {
        //Define $user and $pass
        $user =$conn->real_escape_string($_POST['user']);// you have to use SQL injection for secuirty
        $pass =$conn->real_escape_string($_POST['pass']);// you have to use SQL injection for secuirty
        //sql query to fetch information of registerd user and finds user match.
        $sql="SELECT * FROM userpass WHERE pass='$pass' AND user='$user'";
        $query = mysqli_query($conn,$sql);
        $rows = mysqli_num_rows($query);
        if ( $rows == 1 ) {
            header( "Location: welcome.php" ); // Redirecting to other page
        } else {
            $error = "Username of Password is Invalid";
        }
        mysqli_close($conn); // Closing connection
    }
}
 ?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form action="#" method="post">
    <input type="text" name="user">
    <input type="password" name="pass">
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>
READ ALSO
Followup - Woocommerce different headers for each email types

Followup - Woocommerce different headers for each email types

Objective Replace default email-headerphp for some specific emails with custom email headers

206
My SQL for retrieving connecting trains between 2 stations with travel plan [on hold]

My SQL for retrieving connecting trains between 2 stations with travel plan [on hold]

i have a table in mysql containing trains schedule, I wanted to retrieve a travel plan containing several connecting trains based on a user selected

148
How to get user name from user id in Laravel?

How to get user name from user id in Laravel?

hellow i want to show 10 persons who has deposited and withdraw amount in last24 hoursi am able to show there user_id and amount but i want users name instead of user_id

219
Can&#39;t getting the image to upload on mysql longblob

Can't getting the image to upload on mysql longblob

When I try to upload image, its gets empty fieldI don't know why

157