我需要将数据从两个表Php / Mysql导出到Excel或CSV文件。我尝试使用Union All,但我在一个单元格中获取数据,我需要下一个格式:
Name: Age (from another table)
Levan 22
Dito 35
Giorgi 46
接下来我会收到:
Name:
Levan
Dito
Giorgi
22
35
46
<?php
header("Content-type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename=Export.csv');
//connection
$con = mysql_connect('localhost', 'user', 'pass');
if(!$con) {
echo "Error connection";
}
//select db
$select_db = mysql_select_db('database', $con);
if(!$select_db) {
echo "Error to select database";
}
mysql_set_charset("utf8", $con);
//Mysql query to get records from datanbase
$user_query = mysql_query('SELECT Amount FROM Credit UNION ALL SELECT username FROM User');
//While loop to fetch the records
$contents = "Amount \t username\n";
while($row = mysql_fetch_array($user_query)) {
$contents.=$row['Amount']."\t";
$contents.=$row['username']."\n";
}
$contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
print $contents_final;
?>
答案 0 :(得分:0)
选择 Credit.Amount, User.username 从 信用 INNER JOIN用户在Credit.username = Users.username
上问题是每个表中的用户列不同
贷记卡&GT;用户ID 用户:用户名
试试这个:
选择 Credit.Amount, User.username 从 信用 INNER JOIN用户在Credit.UserID = Users.username
上