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>