I am trying to access my database to get some data, but it keeps returning with the following errors.
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\default.php on line 84
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\default.php on line 86
I have checked the connection and the code is inputting data properly, it's just the results query that won't return any values. Personally, I can't see where the error is because other queries, such as INSERT and CREATE are working perfectly.
mysqli_select_db($conn, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1;";
$result = mysqli_query($conn, $sql);
echo mysqli_num_rows($result); //Line 84
if (mysqli_num_rows($result) > 0) { //Line 86
while($row = mysqli_fetch_assoc($result)) {
...
}
} else {
echo "0 results";
}
?>
If you require any further information, please ask me and I will attempt to provide it.
Full code:
//**Create Connection**//
$conn = mysqli_connect($serverName, $username, $password);
//**Check Connection**//
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
else { echo "
Connected successfully!
"; }//**Create Database**//
$dbName = "myDB";
$sql = "CREATE DATABASE IF NOT EXISTS " . $dbName . " CHARACTER SET utf8 COLLATE utf8_general_ci;";
//Error Handling
if (!mysqli_query($conn, $sql)) { echo "Error creating database: " . mysqli_error($conn); }
else { echo "
Database created successfully!
"; }//**Create Table**//
mysqli_select_db($conn, $dbName);
$tbl_name = "tbl_users";
$sql = "CREATE TABLE IF NOT EXISTS " . $tbl_name . " (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstName VARCHAR(64) NOT NULL, lastName VARCHAR(64) NOT NULL, userEmail VARCHAR(256) NOT NULL, reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP) CHARACTER SET utf8 COLLATE utf8_general_ci;";
//Error Handling
if (!mysqli_query($conn, $sql)) { echo "Error creating table: " . mysqli_error($conn); }
else { echo "
Table '" . $tbl_name . "' created successfully!
"; }mysqli_select_db($conn, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if (!$result = mysqli_query($conn, $sql)) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row . "
";
}
} else {
echo "0 results";
}
解决方案
The problem is here:
You're using mysqli_query() twice in this piece of code:
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if (!$result = mysqli_query($conn, $sql)) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
You need to remove one and do:
if (!$result) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
while adding an escape route else{...} and affected_rows() also.
Which is the reason why your query is failing.
Edit:
This, you're using $conn twice and not using a variable reference for your query:
mysqli_select_db($conn, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
Change it to and using the 4 parameters scheme of mysqli_connect(), since the DB has already been created at this point: (assuming the id of "1" has already been created above that).
$dbName = "myDB";
$conn = mysqli_connect($serverName, $username, $password, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if (!$result) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
else{ echo "Success"; }
or by removing or die(mysqli_error($conn)) and getting the error passed on after, if any.
$dbName = "myDB";
$conn = mysqli_connect($serverName, $username, $password, $dbName);
$sql = "SELECT * FROM tbl_users WHERE id = 1";
$result = mysqli_query($conn, $sql);
if (!$result) {
printf("Errormessage: %s\n", mysqli_error($conn));
}
else{ echo "Success"; }
(additional edit)
You could also try this method and used in conjunction with what I already stated above:
$numrows = mysqli_num_rows($result);
if($numrows > 0){
// do something
}
Add error reporting to the top of your file(s) which will help find errors.
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.