-2

I know there are posts related to the error message "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given". However, I still cannot fathom why my script is not working...grr What does this error mean in terms of my script? How should I go about correcting it?? I am relatively new to php so please by patient with me. I am trying to design a simple log on script.

This is my file cwk_login_tools.

<?php 
    #Makes Logging in Possible by checking for errors
    # Connect  to database or quit
    $dbc = @mysqli_connect ( 'localhost', 'root', '', 'cswk_db' ) OR die ( mysqli_connect_error() ) ;

    function load(){
        header("http://localhost/SecondCswkAttempt/2017%20Web%20Scenario/cwk_index.php");
        exit();
    }

    #Function to check username  and password
    function validate( $dbc, $username = '' , $pwd = '')
    {
        #Initialise errors array
        $errors = array() ;

        #Check a username has been entered
        if ( empty( $username ) ) {
            $errors[] = 'Enter your username .' ;
        } else {
        $u = mysqli_real_escape_string( $dbc , trim( $username) );  
        }

        #Check password has been entered
        if ( empty( $pwd) ) {
            $errors[] = 'Enter your password.' ;
        } else {
        $p = mysqli_real_escape_string( $dbc , trim( $pwd) );   
        }

        #On success retrieve user_id, and username from  the 'users' database
        if ( empty( $errors) ) {
            $q = "SELECT user_id, username, password, FROM users WHERE username = '$u' AND pass = '$p' " ;
            $r = mysqli_query ($dbc, $q ) ;

            #Check password field
            if ( mysqli_num_rows( $r ) == 1 ) {
                $row = mysqli_fetch_array ( $r , MYSQLI_ASSOC) ;
                return array(true, $row ) ;
            }
            #Or on failure set error message
        else {
            $errors[] = 'username  and password not found.' ;   }
        #On failure retrieve error message/s
        return array( false, $errors ); 
        }
    }
    ?>

It would appear that the bit that isn't working are the lines:

if ( mysqli_num_rows( $r ) == 1 ) {
            $row = mysqli_fetch_array ( $r , MYSQLI_ASSOC) ;
            return array(true, $row ) ;
        }

However, this file works with another to make my log in system work. This is the files called cwk_login_action.php:

<?php #PROCESS LOGIN ATTEMPT

#Check form submitted
if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{   
# Connect  to database or quit
$dbc = @mysqli_connect ( 'localhost', 'root', '', 'cswk_db' ) OR die ( mysqli_connect_error() ) ;

    #Get connection, load and validate functions
    require ('cwk_login_tools.php');

    #Check login
    list ( $check, $data ) = validate ( $dbc, $_POST[ 'username' ] , $_POST[ 'pass' ] );

    #On success set session data and display logged in page
    if ( $check ) {
        session_start();
        $_SESSION[ 'user_id' ] = $data[ 'user_id' ];
        $_SESSION[ 'username' ] = $data[ 'username' ];
        load ( 'home.php') ;
    }
    #Or on failure set errors
    else { $errors = $data; }
    #Close database connection
    mysqli_close( $dbc ) ;
}
#Continue to display login page on failure
include ( 'cwk_login.php')
?>
Student
  • 55
  • 1
  • 9

1 Answers1

0

This error means the MySQL query has failed to execute. In this case, it looks like a syntax error:

$q = "SELECT user_id, username, password, FROM users WHERE username = '$u' AND pass = '$p' " ;

You have a comma after "password". Trying removing that to fix the syntax error. Also, you seem to refer to both "password" and "pass" - what is it called in the database? I imagine one of those columns doesn't exist.

Emily Shepherd
  • 1,369
  • 9
  • 20
  • Thank you so much for your response - syntax errors appear to be the bane of my life. I am pretty certain this is the answer and will correct code. Thanks – Student Aug 12 '15 at 11:46