0

Now am trying to export MySQL database to Excel sheet and I am using PHP/MySQL to preform that and every time I get this error I've tried everything but I can't find a solutions for this case

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

And here's the 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;
 }
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

The $result variable is probably false.. meaning that you query is wrong (if you misspelled the table name or something).. or your connection is not established.

use debugger (or var_dump(), print_r().. but use debugger) to find out what are the values stored in $connect and $result variable and you'll probably figure out what went wrong.

  • i just check and the table name was incorrect so i fixed it but now it exports an excel sheet without anything but table rows inside – Mahmoud Azzazy Jan 29 '20 at 13:49
  • like I said.. use debugger.. don't write code blindly, or at least check every line (print the results and check them), see what is happening in your code. all the best :) – Milan Pasic Jan 29 '20 at 13:55
-1

Answer:

Everything seems fine with your code.

$query = "SELECT * FROM tbl_customer";

Please check the table name as everything else looks fine to me.

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

print_r($result); and verify the output.

Zeeshan Eqbal
  • 245
  • 2
  • 11