简体   繁体   English

查询mysql并在PHP中将数据导出为CSV

[英]Query mysql and export data as CSV in PHP

I have myssql db with different tables. 我有不同表的myssql数据库。 The data between the tables are linked and I retrieve and display them by using the userid. 表之间的数据已链接,我使用userid检索并显示它们。 I used the reference from PHP MYSQLi Displaying all tables in a database 我使用了PHP MYSQLi的引用显示数据库中的所有表

But how do I export this information as a csv file? 但是,如何将这些信息导出为csv文件? I have tried instead of echo changed it to a string and print the string to a csv file but it is not working. 我试过而不是用echo将其更改为字符串,然后将字符串打印到csv文件中,但无法正常工作。

I have also tried the example from: http://sudobash.net/php-export-mysql-query-to-csv-file/ 我也尝试过以下示例: http : //sudobash.net/php-export-mysql-query-to-csv-file/

But I can see the data but on top there is also junk (like "<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>" 但是我可以看到数据,但是在顶部也有垃圾(例如"<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>"
etc) inside the csv file. 等)。

Is there another way to do this? 还有另一种方法吗?

If you want to write each MySQL row to a CSV file, you could use the built in PHP5 function fputcsv 如果要将每个MySQL行写入一个CSV文件,可以使用内置的PHP5函数fputcsv

$result = mysqli_query($con, 'SELECT * FROM table');
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

$fp = fopen('file.csv', 'w');

foreach ($row as $val) {
    fputcsv($fp, $val);
}

fclose($fp);

Which should return a comma separated string for each row written to file.csv : 对于写入file.csv每一行,应该返回逗号分隔的字符串:

row1 val1, row1 val2
row2 val1, row2 val2 
etc..

Also be sure to check permissions for the directory you are writing to. 另外,请确保检查要写入的目录的权限。

This is a solution that incorporates the headers or field names by using MySQLi's fetch_fields function - 这是一个使用MySQLi的 fetch_fields函数合并标头或字段名称的解决方案-

$query = "SELECT * FROM table";
$result = $db->query($query);
if (!$result) die('Couldn\'t fetch records');
$headers = $result->fetch_fields();
foreach($headers as $header) {
    $head[] = $header->name;
}
$fp = fopen('php://output', 'w');

if ($fp && $result) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="export.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, array_values($head)); 
    while ($row = $result->fetch_array(MYSQLI_NUM)) {
        fputcsv($fp, array_values($row));
    }
    die;
}

This is also a modification of a different answer where a similar solution was suggested but the headers part did not work for me, so I changed the method of retrieving them. 这也是对不同答案的修改其中提出了类似的解决方案,但标头部分对我不起作用,因此我更改了检索它们的方法。 Credit is due to @Jrgns 归功于@Jrgns

You can try to utilize MySql INTO OUTFILE clause: 您可以尝试利用MySql INTO OUTFILE子句:

SELECT *
  INTO OUTFILE '/tmp/tablename.csv'
       FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
       LINES TERMINATED BY '\n'
   FROM tablename
  WHERE userid = 1

for creating and downloading a csv file from a mysqli query (using object oriented) I think this would help. 从mysqli查询创建和下载csv文件(使用面向对象),我认为这会有所帮助。

This is a Class that connects with the database and the functions will do whatever you want using mysqli and PHP. 这是一个与数据库连接的类,这些函数将使用mysqli和PHP来完成您想要的任何事情。 In this case, calling this class (require or include), just use the "downloadCsv()" function. 在这种情况下,调用此类(要求或包含),只需使用“ downloadCsv()”函数。

As an example, this would be the "class.php" file: 例如,这将是“ class.php”文件:

<?php
class DB{

private $con;

//this constructor connects with the database
public function __construct(){
$this->con = new mysqli("Your_Host","Your_User","Your_Pass","Your_DatabaseName");

if($this->con->connect_errno > 0){
    die('There was a problem [' . $con->connect_error . ']');
    }
}

//create the function that will download a csv file from a mysqli query

public function downloadCsv(){

$count = 0;
$header = "";
$data = "";
//query
$result = $this->con->query("SELECT * FROM Your_TableName");
//count fields
$count = $result->field_count;
//columns names
$names = $result->fetch_fields();
//put column names into header
foreach($names as $value) {
    $header .= $value->name.";";
    }
}
//put rows from your query
while($row = $result->fetch_row())  {
    $line = '';
    foreach($row as $value)       {
        if(!isset($value) || $value == "")  {
            $value = ";"; //in this case, ";" separates columns
    } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . ";"; //if you change the separator before, change this ";" too
        }
        $line .= $value;
    } //end foreach
    $data .= trim($line)."\n";
} //end while
//avoiding problems with data that includes "\r"
$data = str_replace("\r", "", $data);
//if empty query
if ($data == "") {
    $data = "\nno matching records found\n";
}
$count = $result->field_count;

//Download csv file
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=FILENAME.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data."\n";

}
?>

After creating the "class.php" file, in this example, use that function on "download.php" file: 在此示例中,创建“ class.php”文件后,请在“ download.php”文件上使用该函数:

<?php
//call the "class.php" file
require_once 'class.php';
//instantiate DB class
$export = new DB();
//call function
$export->downloadCsv();
?>

After download, open the file with MS Excel. 下载后,使用MS Excel打开文件。

I hope this help you, I think I wrote it well, I didn't feel comfortable with the text and code field. 希望对您有所帮助,我认为我写得不错,对文本和代码字段不满意。

Try this out. 试试看

function addRowToCsv(& $csvString, $cols) {
    $csvString .= implode(',', $cols) . PHP_EOL; //Edits must be 6 characters - all I added was a "." before the =. :-)
}

$csvString = '';
$first = true;

while ($row = mysqli_fetch_assoc($query)) {
    if ($first === true) {
        $first = false;
        addRowToCsv($csvString, array_keys($row));
    }
    addRowToCsv($csvString, $row);
}

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyCsvFile.csv');

echo $csvString;

Notice that the first argument to addRowToCsv is passed by reference. 请注意,addRowToCsv的第一个参数是通过引用传递的。 This is not required and you could easily use a return value, but this is just how I would do it. 这不是必需的,您可以轻松使用返回值,但这就是我要这样做的方式。

If you want to save the output to a file rather than serving it as a download then use the above but replace 如果您想将输出保存到文件中而不是将其用作下载,请使用以上内容,但替换为

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyCsvFile.csv');

echo $csvString;

With.. 用..

file_put_contents('MyCsvFile.csv', $csvString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM