我正在尝试将MySQL数据库中的一些数据写入txt文件。目前我把它放在html的文本区域标签中,但我似乎无法将其转换为txt文件。这就是我作为textarea所得到的:
$pairresult = mysqli_query($conmysql,"SELECT * FROM orders WHERE order_number IN ( SELECT order_number FROM orders GROUP BY order_number HAVING count(ordern_number) > 1 ) ORDER BY order_number");
echo "<h4>Customers with Multiple Orders</h4><textarea rows=\"5\" cols=\"90\" readonly>";
while($row = mysqli_fetch_array($pairresult))
{
echo str_replace("&","\&",$row['shipment_address_name']) . "\n";
echo str_replace("&","\&",$row['shipment_address_street']) . "\n";
if ( !empty($row['shipment_address_street_2']) ) {
echo str_replace("&","\&",$row['shipment_address_street_2']) . "\n";
} else {
}
echo str_replace("&","\&",$row['shipment_address_city']) . "\n";
echo str_replace("&","\&",$row['shipment_address_postal_code']) . "\n";
if ( !empty($row['customer_phone']) ) {
echo str_replace("&","\&",$row['customer_phone']) . "\n";
} else {
}
echo "(" . $row['fulfillment_line_item_id'] . ")\n";
echo "[" . $row['item_name'] . "]\n\n";
}
echo "</textarea><br />";
我在脚本的另一部分有一个类似的位,但没有成功写入txt文件的if语句等:
$pairresult = mysqli_query($conmysql,"SELECT * FROM orders WHERE order_number IN ( SELECT order_number FROM orders GROUP BY order_number HAVING count(order_number) > 1 ) ORDER BY order_number");
while($row = mysqli_fetch_array($pairresult))
{
$multiple = "productmultiple_tracking($filetimestamp).csv";
$handle = fopen($grouponmultiple, 'a') or die('Cannot open file: '.$multiple);
$data = "\"" . $row['fulfillment_line_item_id'] . "\",\"" . $row['fulfillment_line_item_id'] . "\",\"YDL\",\"\"\n";
fwrite($handle, $data);
fclose($handle);
}
echo "<a href=\"$multiple\">Download Template</a><br />";
我如何将textarea中的那个位以相同的格式输入到txt文件?
答案 0 :(得分:1)
在第一个示例中将所有echo
替换为$data .=
,这样您的文本就会显示在$ data变量中。在保存$ data之后,如第二个例子中那样。
答案 1 :(得分:0)
<?php
$pdo = new PDO(...);
$results = $pdo->query("SELECT * FROM myTable INTO OUTFILE 'data.txt'");
$dummy = $result->fetchAll();
data.txt文件将写在MySQL服务器上。该目录必须可由mysqld进程的uid写入。它不会覆盖任何现有文件,并要求您具有FILE SQL权限。
第二种选择:使用mysqldump输出到平面文本文件(如提到的@OMG Ponies):
mysqldump -t -T <directory> <database> <table>
这就像INTO OUTFILE一样,需要在MySQL服务器主机上运行,目录必须是mysqld uid可写的。
第三个选项:使用mysql客户端和输出文本运行查询:
mysql -B -e "SELECT * FROM MyTable" <database> > mytable.txt
这可以在任何主机上运行,并且不需要特殊权限或目录权限。但是可能无法处理NULL,就像使用mysqldump或INTO OUTFILE一样。