• PHP Help PHP Newbies
  • [RESOLVED] Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean

Sorry stuck again!

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\test_council_site\login.php on line 52

can someone also explain this error message to me

<?php // This page will allow users to log into the site

// get the databse settings file and estblish a connection.

require_once ('../mysqli_connect.php');

// set the page title variable $page_title

$page_title = 'User Login';

// get the page header file from the includes directory

include ('includes/header.html');

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

$errors = array(); // Initialize an error array.

// check the email address is present
if (empty($_POST['email_address'])) {
				$errors[] = 'You forgot to enter your email address.';
	} else {
	$email_address = mysqli_real_escape_string ($dbc, $_POST['email_address']);

}

// check the password

if (empty($_POST['password'])) {
				$errors[] = 'You forgot to enter your password.';
	} else {
	$password = mysqli_real_escape_string ($dbc, $_POST['password']);

}

// check password and email_address have variables assigned

if ($email_address && $password){

// submit the values in a query to the mysql database

$q = "SELECT user_id, first_name, user_level FROM user_details WHERE (email='$email_address' AND pass=SHA1'$password'))";

//run the query or provide an error message as to why it wasnt run. 
$r = @mysqli_query ($dbc, $q); 

// if mysql_error display the Query+Error 
if(mysql_error()) exit($q.'<br>'.mysql_error()); 

//check a match was made
if (mysqli_num_rows($r) == 1) {

// register the values to the sessiobn and redirect the user to the homepage.

$_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC);

// free all memory associated with the result identifier 

mysqli_free_results($r);

//close the database connection

mysqli_close($dbc);

// re direct using the header command

header('Location: http://localhost/test_council_site/index.php');

// end the script

exit();

	} else { // No match was made.
		echo '<p class="error">Either the email address and password entered do not match those on file or you have not yet activated your account.</p>';

	}

} else { // If everything wasn't OK.
		echo '<h1>Error!</h1>
	<p class="error">The following error(s) occurred:<br />';
	foreach ($errors as $msg) { // Print each error.
		echo " - $msg<br />\n";
	}
	echo '</p><p>Please try again.</p><p><br /></p>';

}

mysqli_close($dbc);

} // End of SUBMIT conditional.
?>

<h1>Login</h1>
<p>Your browser must allow cookies in order to log in.</p>
<form action="login.php" method="post">
	<fieldset>
	<p>Email Address: <input type="text" name="email_address" size="20" maxlength="80" value="<?php if (isset($_POST['email_address'])) echo $_POST['email_address']; ?>"  /> </p>
	<p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>
	<div align="center"><input type="submit" name="submit" value="Login" /></div>
	<input type="hidden" name="submitted" value="TRUE" />
	</fieldset>
</form>

<?php // Include the HTML footer.
include ('includes/footer.html');
?>

    your query $ q failed and is returning false, the boolean value mentioned in the error notice.

    you are also checking for mysql error, not mysqli, you can mix the two up

      // problem: pass=SHA1'$password')
      $q = "SELECT user_id, first_name, user_level FROM user_details 
      WHERE (email='$email_address' AND pass=SHA1'$password'))";
      
      
      //This may work:
      $q = "SELECT user_id, first_name, user_level FROM user_details 
      WHERE (email='$email_address' AND pass=SHA1('$password'))";
      
      //But I would use PHP for sha1(), like this:
      $sha1_pass = sha1($password);
      $q = "SELECT user_id, first_name, user_level FROM user_details 
      WHERE (email='$email_address' AND pass='$sha1_pass')";

      Also as pointed out, we need to change to mysqli_error()
      Like this

      //run the query or provide an error message as to why it wasnt run.
      $r = @mysqli_query ($dbc, $q);
      
      // mysqli_error display the Query+Error
      if(mysqli_error()) exit($q.'<br>'.mysqli_error());
      
      //check a match was made
      if (mysqli_num_rows($r) == 1) {
      

        Ok tyanks for the reply I have updated the vode and tried to log in but i get the erro message below.

        Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\test_council_site\login.php on line 53

        Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\test_council_site\login.php on line 56

        my code is now (ps - thanks for everyones help I am very new to this and appreciate it!)

        <?php // This page will allow users to log into the site
        
        // get the databse settings file and estblish a connection.
        
        require_once ('../mysqli_connect.php');
        
        // set the page title variable $page_title
        
        $page_title = 'User Login';
        
        // get the page header file from the includes directory
        
        include ('includes/header.html');
        
        // Check if the form has been submitted:
        if (isset($_POST['submitted'])) {
        
        $errors = array(); // Initialize an error array.
        
        // check the email address is present
        if (empty($_POST['email_address'])) {
        				$errors[] = 'You forgot to enter your email address.';
        	} else {
        	$email_address = mysqli_real_escape_string ($dbc, $_POST['email_address']);
        
        }
        
        // check the password
        
        if (empty($_POST['password'])) {
        				$errors[] = 'You forgot to enter your password.';
        	} else {
        	$password = mysqli_real_escape_string ($dbc, $_POST['password']);
        
        }
        
        // check password and email_address have variables assigned
        
        if ($email_address && $password){
        
        
        // set up the password variable 
        $sha1_pass = sha1($password);
        
        // submit the values in a query to the mysql database
        $q = "SELECT user_id, first_name, user_level FROM user_details
        WHERE (email='$email_address' AND password='$sha1_pass')"; 
        
        //run the query or provide an error message as to why it wasnt run.
        $r = @mysqli_query ($dbc, $q);
        
        // mysqli_error display the Query+Error
        if(mysqli_error()) exit($q.'<br>'.mysqli_error());
        
        //check a match was made
        if (mysqli_num_rows($r) == 1) { 
        
        // register the values to the sessiobn and redirect the user to the homepage.
        
        $_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC);
        
        // free all memory associated with the result identifier 
        
        mysqli_free_results($r);
        
        //close the database connection
        
        mysqli_close($dbc);
        
        // re direct using the header command
        
        header('Location: http://localhost/test_council_site/index.php');
        
        // end the script
        
        exit();
        
        	} else { // No match was made.
        		echo '<p class="error">Either the email address and password entered do not match those on file or you have not yet activated your account.</p>';
        
        	}
        
        } else { // If everything wasn't OK.
        		echo '<h1>Error!</h1>
        	<p class="error">The following error(s) occurred:<br />';
        	foreach ($errors as $msg) { // Print each error.
        		echo " - $msg<br />\n";
        	}
        	echo '</p><p>Please try again.</p><p><br /></p>';
        
        }
        
        mysqli_close($dbc);
        
        } // End of SUBMIT conditional.
        ?>
        
        <h1>Login</h1>
        <p>Your browser must allow cookies in order to log in.</p>
        <form action="login.php" method="post">
        	<fieldset>
        	<p>Email Address: <input type="text" name="email_address" size="20" maxlength="80" value="<?php if (isset($_POST['email_address'])) echo $_POST['email_address']; ?>"  /> </p>
        	<p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>
        	<div align="center"><input type="submit" name="submit" value="Login" /></div>
        	<input type="hidden" name="submitted" value="TRUE" />
        	</fieldset>
        </form>
        
        <?php // Include the HTML footer.
        include ('includes/footer.html');
        ?>
        

          We need to put $dbc to like this: mysqli_error($dbc)
          Now it should work 🙂
          Or at least you will get an mysqli_error report telling what is wrong...
          Also I dont think you need parentes around this:
          (email='$email_address' AND password='$sha1_pass')

          // set up the password variable
          $sha1_pass = sha1($password);
          
          // submit the values in a query to the mysql database
          $q = "SELECT user_id, first_name, user_level FROM user_details
          WHERE email='$email_address' AND password='$sha1_pass'";
          
          //run the query or provide an error message as to why it wasnt run.
          $r = @mysqli_query ($dbc, $q);
          
          // mysqli_error display the Query+Error
          if(mysqli_error($dbc)) exit($q.'<br>'.mysqli_error($dbc));
          
          //check a match was made
          if (mysqli_num_rows($r) == 1) {
          
          // register the values to the sess

            Just to show I am learning I have resolved the error above.

            Just for info

            $q = "SELECT user_id, first_name, user_level FROM user_details
            WHERE (email='$email_address' AND password='$sha1_pass')";

            Should of been

            $q = "SELECT user_id, first_name, user_level FROM user_details
            WHERE (email_address='$email_address' AND password='$sha1_pass')";

            And

            mysqli_free_results($r);

            Should of been

            mysqli_free_result($r);

              Thanks Dragon but for a newbie the manual isnt the easiest starting block.

                read my last post above with some small changes
                thanks

                  halojoy you are a star, and I understand the problem so I wont make the same mistake again!

                  Thanks thanks and thanks

                    Write a Reply...