【问题标题】:How to export query result to csv file?如何将查询结果导出到 csv 文件?
【发布时间】:2017-02-25 13:52:06
【问题描述】:

是否可以由此创建 csv 文件?

数据库中的这个表看起来像下面的一个回显

    id  | category|  supplier   | price
    1   |    A    |  supplier1  | 5
    2   |    B    |  supplier2  | 3
    3   |    B    |  supplier1  | 7
    4   |    C    |  supplier3  | 6
    5   |    C    |  supplier1  | 2
    6   |    B    |  supplier3  | 9
<?php
session_start();
$host = "localhost";
$user = "root";
$pass = "";

$con = mysql_connect($host,$user,$pass);
if($con)
{
      $db = mysql_select_db('stocks');
       if($db)
       {}
       else 
       {
         echo "No Database Found! " ;
        }
}
else 
{
     echo "Failed to Connect! ";
}
?>
<table>
<tr>
    <th>Category</th>
    <th>Stock</th>
    <th>Percentage</th>
</tr>
<?php
$total=6;
        $q="SELECT id,name,supplier,price,COUNT(*) FROM  items GROUP BY supplier";
        $r = mysql_query($q);
        while($row = mysql_fetch_array($r))
        {
                 $ratio=($row['COUNT(*)']/$total)*(100);
            echo "<tr>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['COUNT(*)'] . "</td>";
echo "<td>" . round($ratio,2). "%</td>";
echo "</tr>";
        }
echo "</table>";        
         ?>

如何将其放入 csv 中?

我只知道在表格中全选。

类别库存百分比 1 16.67% 乙 3 50% C 2 33.33%

【问题讨论】:

  • @ 使用 toad for oracle 你可以做到这一点.. 如果是的话,该选项对你有帮助吗?我会告诉你怎么做
  • @Gordon 谢谢你,但我是编程新手,只是偶然发现了 table-csv 主题,我想知道它是否也能做到这一点会非常有用

标签: php mysql sql database export-to-csv


【解决方案1】:

您可以为此使用 php 函数 fputcsv。我正在分享一个示例工作代码。

$output = fopen('result.csv', 'w');

$rs=mysql_query($sql,$conn);

while($row = mysql_fetch_assoc($rs)) {
    fputcsv($output, $row);
}

【讨论】:

  • 它可以工作,但它输出数据库中的所有内容如何使其看起来像上面的回声
【解决方案2】:
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

// fetch the data
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$rows = mysql_query('SELECT field1,field2,field3 FROM table');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

链接到页面http://code.stephenmorley.org/php/creating-downloadable-csv-files/希望能满足你的需要

【讨论】:

  • 但是 $ratio 不在数据库表中?好的,我会尝试的,希望可以
  • 它输出表中的所有内容
  • 在 select 查询中使用 isnull(column_name,'0') 作为 column_name 来处理空值,这是消除错误的最后一个技巧
【解决方案3】:
while($row = mysql_fetch_array($r))
        {   $data=array($row['category'],$row['COUNT(*)'],round(($row['COUNT(*)']/$total)*(100),2));
            fputcsv($output, $data);
        }

@Davinder @Virgilio 这完全有效,但在第 39 行的 C:\xampp\htdocs\revamp\csv.php 中有错误消息(解析错误:语法错误,意外')') 除了它完美无缺

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 1970-01-01
    • 2015-09-12
    • 2023-04-04
    • 2017-12-31
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多