0

I'm a beginner in regards to PHP & MySQLi queries.

From other similar problems that users have had like me, this error usually relates to an incorrect formatting of the $query command. I have double-checked, and all columns (firstname etc) exist in the 'member' table, and I believe the query is formatted correctly? Can anyone shed more light on where I'm going wrong here? Thanks.

I am receiving the following error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\...\UniServerZ\www\BIT608\dbstuff-SELECT.php on line 22

Line 22 of my code is: if (mysqli_num_rows($result) > 0) {

And the full code I have is:

<html><head><title>MySQL examples</title> </head>
<body>
<?php
include "config.php"; //load in any variables
$DBC = mysqli_connect("127.0.0.1", DBUSER , DBPASSWORD, DBDATABASE);

//check if the connection was good
if (!$DBC) {
    echo "Error: Unable to connect to MySQL.\n". mysqli_connect_errno()."=".mysqli_connect_error() ;
    exit; //stop processing the page further
};

//insert DB code from here onwards

echo "<pre>";  
//prepare a query and send it to the server
$query = 'SELECT memberID,firstname,lastname,email FROM member';
$result = mysqli_query($DBC,$query);

//check result for data
if (mysqli_num_rows($result) > 0) {
    /* retrieve a row from the results
       one at a time until no rows left in the result */
    echo "Record count: ".mysqli_num_rows($result).PHP_EOL;
    while ($row = mysqli_fetch_assoc($result)) {
      echo "member ID ".$row['memberID'] . PHP_EOL;
      echo "Firstname ".$row['firstname'] . PHP_EOL;
      echo "Lastname ".$row['lastname'] . PHP_EOL;
      echo "Email ".$row['email'] . PHP_EOL;
      echo "<hr />";
   }
   mysqli_free_result($result); //free any memory used by the query
}
echo "</pre>";

/* show a quick confirmation that we have a connection
   this can be removed - not required for normal activities
*/
    echo "Connected via ".mysqli_get_host_info($DBC); //show some info on the connection 

mysqli_close($DBC); //close the connection once done
?>
</body>
</html>

ootz1986
  • 1
  • 2
  • If you are only starting with PHP learn PDO instead of MySQLi or find a better tutorial. – Dharman Mar 18 '20 at 02:00
  • You haven't enabled error reporting, so you can't know what the issue is. We can't tell you either. – Dharman Mar 18 '20 at 02:01
  • @Dharman - you're first response helped alot. Using the top answer from your response, I added ```mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);``` to the beginning og the page, and it did confirm there was a syntax issue on line 22. I rechecked the documentation for MySQLi and the solution was simple - I needed to put backticks on all columns & tables in the $query. A $query reading as: $query = "SELECT `memberID`,`firstname`,`lastname`,`email` FROM `member`"; solved this problems. Thanks @Dharman!! – ootz1986 Mar 18 '20 at 02:11

0 Answers0