dsyua2828 2017-03-09 23:04
浏览 56

PHP在数据库中回显表并将其下载为CSV文件

I'm new to PHP and am trying to create a small snippet of code that reads the tables in my database and allows the user to download the tables into a CSV file.

So far, I've been able to connect to my database and echo through the tables

 // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed1: " . $conn->connect_error);
} 

// SQL query
$sql = "SHOW TABLES IN `abc1`";

// perform the query and store the result
$result = $conn->query($sql);

// if the $result not False, and contains at least one row
     if($result !== false) {

       // if at least one table in result
       if($result->num_rows > 0) {
       // traverse the $result and output the name of the table(s)
            while($row = $result->fetch_assoc()) {
                 echo '<br />'. $row['Tables_in_abc1'];
            }
       }
 else echo 'There is no table in "tests"';
 }
 else echo 'Unable to check the "tests", error - '. $conn->error;


 $conn->close();
 ?>

Now I'd like to turn each table into a link so when the user clicks on it, they will be able to download the table's data into a CSV file.

How do I do that?

  • 写回答

2条回答 默认 最新

  • dora12345678 2017-03-09 23:07
    关注

    This should be a comment but I'm not high enough of a level to leave one. You should check out PHPExcel.

    https://github.com/PHPOffice/PHPExcel

    It comes with numerous examples which should help you achieve what you are trying to do.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?