Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么它说布尔值是给定的_Php_Json_Database_Boolean - Fatal编程技术网

Php 为什么它说布尔值是给定的

Php 为什么它说布尔值是给定的,php,json,database,boolean,Php,Json,Database,Boolean,有人能帮我吗。我正试图从表中提取所有数据。但它一直以布尔形式返回 <?php $con = mysqli_connect("localhost", "root", "", "student"); $query = "SELECT * FROM `announcement` ORDER BY `announce_id` DESC"; $result = mysqli_query($con, $query); while($row = mysqli_fet

有人能帮我吗。我正试图从表中提取所有数据。但它一直以布尔形式返回

<?php
    $con = mysqli_connect("localhost", "root", "", "student");

    $query = "SELECT * FROM `announcement` ORDER BY `announce_id` DESC";

    $result = mysqli_query($con, $query);

    while($row = mysqli_fetch_assoc($result)){
        $data[] = $row;
    }

    echo json_encode($data);
?>

请确保“公告”表中有数据

可以使用mysqli_num_rows()函数检查结果集中返回的行数

试试这个:

$con = mysqli_connect("localhost", "root", "", "student");
$query = "SELECT * FROM `announcement` ORDER BY `announce_id` DESC";
$result = mysqli_query($con, $query);

$rowcount = mysqli_num_rows($result); //returns number of rows

if($rowcount > 0) {
  while($row = mysqli_fetch_assoc($result)){
    $data[] = $row;
  }
  echo json_encode($data);
}

如有任何疑问/担忧,请告知我们

因为mysqli_查询返回false,$result包含bool(false),mysqli_fetch_assoc需要一个mysqli_result对象,当您给它一个bool时,它会抱怨。我们不知道为什么mysqli_查询返回false,而且由于您没有启用错误报告,PHP不会告诉您为什么。您需要在代码中进行错误检查,以便更早地捕捉到这一点,并让php告诉您出了什么问题。最简单的方法是:用

<?php
error_reporting(E_ALL);
if(!mysqli_report(MYSQLI_REPORT_ALL)){
throw new RuntimeException("unable to set mysqli_report to MYSQLI_REPORT_ALL !");
}

现在,每当出现错误时,php都应该给出详细的报告,包括为什么mysqli_查询返回false。

始终检查错误
($con,$query)或die($con->error)
如果查询失败,则返回布尔值false。可能重复的问题是查询错误或连接错误,而不是空表
<?php
error_reporting(E_ALL);
if(!mysqli_report(MYSQLI_REPORT_ALL)){
throw new RuntimeException("unable to set mysqli_report to MYSQLI_REPORT_ALL !");
}
function exception_error_handler($severity, $message, $file, $line) {
    if (!(error_reporting() & $severity)) {
        // This error code is not included in error_reporting
        return;
    }
    throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");