Export and download my php table data to csv

帅比萌擦擦* 提交于 2020-03-06 10:25:38

问题


I'm trying to get my datbase fetched data to Excel file for downloading it in execl or csv, but I'm having problems with exporting and also No datas are fetching to the csv .Here is my code:

This is my php code

 <?php  
 $connect = new PDO('mysql:host=localhost;dbname=123', '123', 'invoice123');
 $query ="SELECT * from tbl_order";  
 $result = mysqli_query($connect, $query);  
 ?>

this is my html

<form method="post" action="export.php" align="center">  
                     <input type="submit" name="export" value="CSV Export" class="btn btn-success" />  
                </form>  
      <table id="data-table" class="table table-bordered table-striped">
        <thead>
          <tr style="background-color: cornflowerblue;">
            <th>Invoice No.</th>
            <th>Invoice Date</th>
            <th>Student Name</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>

          </tr>
        </thead>
        <?php  
                     while($row = mysqli_fetch_array($result))  
                     {  
                     ?> 

              <tr>
                 <td><?php echo $row["order_id"]; ?></td>
                <td><?php echo $row["order_no"]; ?></td>
                <td><?php echo $row["order_date"]; ?></td>
                <td><?php echo $row["order_receiver_name"]; ?></td>
                <td><?php echo $row["order_total_before_tax"]; ?></td>
                <td><?php echo $row["order_total_tax1"]; ?></td>
                <td><?php echo $row["order_total_tax2"]; ?></td>
                <td><?php echo $row["order_total_tax"]; ?></td>
                <td><?php echo $row["order_total_after_tax"]; ?></td>
                <td><?php echo $row["order_datetime"]; ?></td>

              </tr>
                <?php       
                     }  
             ?> 

      </table>

    </div>
    <br>

this is my export.php page

 <?php  
      //export.php  
 if(isset($_POST["export"]))  
 {  
      $connect = new PDO('mysql:host=localhost;dbname=123', '123', 'invoice123');
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=data.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('
Invoice No.', 'Invoice Date', 'Student Name', 'Total Amount'));  
      $query = "SELECT * from tbl_order";  
      $result = mysqli_query($connect, $query);  
      while($row = mysqli_fetch_assoc($result))  
      {  
           fputcsv($output, $row);  
      }  
      fclose($output);  
 }  
 ?>

I need to view these datas on my webpage and also i need to export and download it.


回答1:


Here you go!

change

 $connect = new PDO('mysql:host=localhost;dbname=123', '123', 'invoice123');

to

 $connect = mysqli_connect('localhost','123','invoice123','123');

and it should work, you can't use "PDO" with mysqli_query because both are different ways to connect to the database.

You can read more about PDO and mysqli here ->

https://websitebeaver.com/php-pdo-vs-mysqli

You should also enable error reporting. because when I tested the PHP codes it returned an errror.



来源:https://stackoverflow.com/questions/60183209/export-and-download-my-php-table-data-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!