简体   繁体   English

使用 PHP 从两个表中将 MYSQL 导出到 .csv

[英]Export MYSQL to .csv from two tables with PHP

I got 2 tables Tickets and Replies .我有 2 张桌子的回复

Tickets got this fields: id, dname, mname, subject, priority, message, date, last_reply, status, close_mname, cdfileds Tickets 有这个字段: id、dname、mname、subject、priority、message、date、last_reply、status、close_mname、cdfileds

Replies got this fileds: id, tid, mname, message, date回复得到了这个文件: id、tid、mname、message、date

The tables are a support tickets.这些表是支持票。 I want to export tables to CSV.我想将表格导出为 CSV。 This is the code I use.这是我使用的代码。

<?php
/* vars for export */
// database record to be exported
$db_record = 'trxp_tickets';
$db_record2 = 'trxp_replies'; // added by me
// optional where query
$where = 'WHERE 1 ORDER BY 1';
// filename for export
$csv_filename = 'db_export_'.$db_record.'_'.date('Y-m-d').'.csv';
// database variables
$hostname = "localhost";
$user = "xxxxx";
$password = "xxxxx";
$database = "xxxxxx";
// Database connecten voor alle services
mysql_connect($hostname, $user, $password)
    or die('Could not connect: ' . mysql_error());

mysql_select_db($database)
    or die ('Could not select database ' . mysql_error());
// create empty variable to be filled with export data
$csv_export = '';
// query to get data from database
$query = mysql_query("SELECT id,dname,mname,subject,priority,message,date,last_reply,status,close_mname,cdfields FROM ".$db_record." ".$where);
$field = mysql_num_fields($query);
// create line with field names
for($i = 0; $i < $field; $i++) {
    $csv_export.= mysql_field_name($query,$i).';';
}
// newline (seems to work both on Linux & Windows servers)
$csv_export.= '
';
// loop through database query and fill export variable
while($row = mysql_fetch_array($query)) {
    // create line with field values
    for($i = 0; $i < $field; $i++) {
        $csv_export.= '"'.$row[mysql_field_name($query,$i)].'";';
    }   
    $csv_export.= '
';  
}
// Export the data and prompt a csv file for download
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=".$csv_filename."");
echo($csv_export);
?>

In the code, it is not included in the second table when I make the SELEC, For doubles results.在代码中,当我进行 SELEC 时,它不包含在第二个表中,对于双打结果。 Try using UNION and UNION ALL but did not work.尝试使用 UNION 和 UNION ALL 但没有用。 I would like to stay in the CSV listed below each ticket responses.我想留在每个票响应下面列出的 CSV 中。

Alter your SQL to get data from both tables:更改 SQL 以从两个表中获取数据:

$query = mysql_query("SELECT 
                          r1.id, r1.dname, r1.mname, r1.subject,
                          r1.priority, r1.message,
                          DATE_FORMAT(FROM_UNIXTIME(r1.date), '%d/%m/%Y') as r1_date,
                          r1.last_reply, r1.status, r1.close_mname,
                          r1.cdfields,
                          r2.id, r2.tid, r2.mname,
                          r2.message
                          DATE_FORMAT(FROM_UNIXTIME(r2.date), '%d/%m/%Y') as r2_date,
                     FROM ".$db_record." r1
               INNER JOIN ".$db_record2." r2 
                       ON r1.id = r2.tid
                          ".$where.");

This should do the trick.这应该可以解决问题。

mysql_* functions are deprecated.不推荐使用 mysql_* 函数。 You should change to mysqli_* or PDO.您应该更改为 mysqli_* 或 PDO。 ( http://php.net/manual/en/function.mysql-query.php ) ( http://php.net/manual/en/function.mysql-query.php )

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

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