php mysql查询无结果集_[PHP + MySQL]数据库SELECT查询未返回结果

bd96500e110b49cbb3cd949968f18be7.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值