[PHP + MySQL]数据库SELECT查询未返回结果 [英] [PHP + MySQL]Database SELECT query returning no result
问题描述
我正在尝试访问我的数据库以获取一些数据,但是它不断返回以下错误.
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
我已经检查了连接,并且代码正在正确输入数据,只是结果查询不会返回任何值.就个人而言,我看不到错误在哪里,因为其他查询(例如INSERT和CREATE)运行良好.
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.
<?php
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.
完整代码:
//**Create Connection**//
$conn = mysqli_connect($serverName, $username, $password);
//**Check Connection**//
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
else { echo "<p>Connected successfully!</p>"; }
//**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 "<p>Database created successfully!</p>"; }
//**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 "<p>Table '" . $tbl_name . "' created successfully!</p>"; }
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 . "<br>";
}
} else {
echo "0 results";
}
推荐答案
问题在这里:
您在这段代码中两次使用了mysqli_query()
:
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));
}
同时添加逃生路线else{...}
和 affected_rows()
也.
while adding an escape route else{...}
and affected_rows()
also.
- 这是查询失败的原因.
这是您使用$conn
两次,而不是在查询中使用变量引用:
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));
将其更改为并使用 mysqli_connect()
,因为此时已经创建了数据库:(假设ID上面已经创建了ID"1").
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 die(mysqli_error($conn))
并让错误继续传递(如果有的话).
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"; }
(其他修改)
您也可以尝试此方法,并将其与我上面已经说过的结合使用:
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.
<?php
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.
这篇关于[PHP + MySQL]数据库SELECT查询未返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!