为什么我返回的是布尔值而不是 SQL 变量

为什么我返回的是布尔值而不是 SQL 变量

我已经将 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;
        }

相关内容