0

我正在尝试将 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 文件中?

4

2 回答 2

1

在第一个示例中全部替换echo$data .=,因此您将在 $data 变量中包含文本。在它保存 $data 之后,如第二个示例所示。

于 2013-09-13T09:16:07.640 回答
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 那样处理。

于 2013-09-13T09:21:13.207 回答