正在将 MySQL 数据库导出到 Excel

Exporting MySQL database to Excel

现在我正在尝试将 MySQL 数据库导出到 Excel sheet 并且我正在使用 PHP/MySQL 来执行它 每次我收到这个错误 我已经尝试了所有方法,但找不到针对此案例的解决方案

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in staff/export.php on line 9

这是 export.php

<?php  
//export.php  
$connect = mysqli_connect("localhost", "user", "password, "dbname");
$output = '';
if(isset($_POST["export"]))
{
 $query = "SELECT * FROM tbl_customer";
 $result = mysqli_query($connect, $query);
 if(mysqli_num_rows($result) > 0)
 {
  $output .= '
   <table class="table" bordered="1">  
<tr><th>Id</th><th>Customer name</th><th>Agent name</th><th>Phone Number</th><th>Email</th><th>Brand Name</th><th>Field</th><th>Website</th><th>comments</th></tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
    <tr>  
                         <td>'.$row["Id"].'</td>  
                         <td>'.$row["Customer name<"].'</td>  
                         <td>'.$row["Agent name"].'</td>  
       <td>'.$row["Phone Number"].'</td>  
       <td>'.$row["Email"].'</td>
              <td>'.$row["Brand Namel"].'</td>
        <td>'.$row["Field"].'</td>
        <td>'.$row["Website"].'</td>
         <td>'.$row["comments"].'</td>
                    </tr>
   ';
  }
  $output .= '</table>';
  header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename=download.xls');
  echo $output;
 }
}
?>

$result 变量可能是 false.. 这意味着您的查询是错误的(如果您拼错了 table 名称或其他内容).. 或者您的连接未建立。

使用调试器(或 var_dump()、print_r().. 但使用调试器)找出存储在 $connect 和 $result 变量中的值是什么,您可能会找出问题所在。

答案:

您的代码看起来一切正常。

$query = "SELECT * FROM tbl_customer";

请检查 table 名称,因为我觉得其他一切都很好。

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

print_r($result); 并验证输出。