将我的php表数据导出并下载到csv [英] Export and download my php table data to csv

查看:136
本文介绍了将我的php表数据导出并下载到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将datbase提取的数据获取到Excel文件中以在execl或csv中下载,但是我在导出时遇到问题,并且也没有数据正在获取到csv中.这是我的代码:

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:

这是我的php代码

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

这是我的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>

这是我的export.php页面

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.

推荐答案

去这里!

更改

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

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

它应该工作,您不能将"PDO"与mysqli_query一起使用,因为两者都是连接数据库的不同方法.

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

您可以在此处了解有关PDO和mysqli的更多信息->

You can read more about PDO and mysqli here ->

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

您还应该启用错误报告.因为当我测试PHP代码时,它返回了一个错误.

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

这篇关于将我的php表数据导出并下载到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆