![为什么我返回的是布尔值而不是 SQL 变量](https://linux22.com/image/1607242/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E8%BF%94%E5%9B%9E%E7%9A%84%E6%98%AF%E5%B8%83%E5%B0%94%E5%80%BC%E8%80%8C%E4%B8%8D%E6%98%AF%20SQL%20%E5%8F%98%E9%87%8F.png)
我已经将 IP 地址存储在一个表中,并尝试将它们打印在我的网页上
http://freegifts.virtualworker.online/verify.php
但我得到了这个错误
警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,/homepages/31/d784186148/htdocs/clickandbuilds/GiveawaysandFreebiesbyJoel/verify.php 第 22 行中给出的值为 null
奇怪的是,如果我使用访问者表而不是我需要的表,它就可以工作。
这是我的代码
<?php
include_once 'dbh.inc.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
//Here is an example where we use the connection to take data from our database, and show it in the browser
//Here we mix PHP and SQL in order to have a statement ready that we can refer to later on
//Here we "query" the SQL statement in the database using our connection variable
$result = mysqli_query($con,"SELECT * FROM postback");
//Here we get the number of results the query returned from the database
$resultCheck = mysqli_num_rows($result);
echo "1";
//We then check if we had atleast 1 result from the database
if ($resultCheck > 0) { echo $resultCheck;
//If we had a result, then we use a while loop to spit out our rows of data, one by one
//At the same time we also assign the database data to a variable named $row
while ($row = mysqli_fetch_assoc($result)) {
//We can spit out the data by refering to our database column names
echo $row['ufirst'] . "<br>";
}
}
else echo "3";
?>
</body>
</html>
错误是 $resultCheck = mysqli_num_rows($result);
答案1
来自文档:
失败时返回 FALSE。对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回 mysqli_result 对象。对于其他成功的查询,mysqli_query() 将返回 TRUE。
您必须在任何操作之前测试返回值并显示错误代码。
if (!$result = mysqli_query($con,"SELECT * FROM postback")) {
printf("Error: %s\n", $mysqli->error);
exit;
}