1

我正在尝试从数据库中访问超过 8000 个 30 列的记录,并写入由制表符分隔的文本文件。我的PHP代码是:

 $fp1 = fopen( 'obspg.txt', 'w' );
  fwrite( $fp1,"RETSKU\tProduct Title\tDetailed Description\tProduct Condition\tSelling Price\tAvailability\tProduct URL\tImage URL\tManufacturer Part Number\tManufacturer Name\tCategorization\tGender\tsize\tColor\n");
  //retrive records from database and write to file
  $result = mysqli_query($con,"SELECT * FROM `TABLE 1` ");
  while($row = mysqli_fetch_array($result))
  {
     fwrite($fp1,$row[`id`]."\t".$row[`title`]."\t".  $row[`description`]."\t".$row[`condition`]."\t". $row[`price`]."\t".$row[`availability`]."\t".$row[`link`]."\t". $row[`image_link`]."\t".$row[`mpn`]."\t".$row[`brand`]."\t".$row[`google_product_category`]."\t".$row[`Gender`]."\t".$row[`size`]."\t".$row[`Color`]."\n");
  } 
  fclose( $fp1 );

但是执行时间太长。如何提高代码的性能?我不能在我的服务器上使用超过 25% 的使用率。

4

1 回答 1

2

构造您要写入变量的数据,然后对文件进行一次写入。它的效率要高得多。

$fp1 = fopen( 'obspg.txt', 'w' );

$outPut = "RETSKU\tProduct Title\tDetailed Description\tProduct Condition\tSelling Price\tAvailability\tProduct URL\tImage URL\tManufacturer Part Number\tManufacturer Name\tCategorization\tGender\tsize\tColor\n";

//retrive records from database and write to file
$result = mysqli_query($con,"SELECT * FROM `TABLE 1` ");
while($row = mysqli_fetch_array($result))
{
 $outPut .= $row[`id`]."\t".$row[`title`]."\t".  $row[`description`]."\t".$row[`condition`]."\t". $row[`price`]."\t".$row[`availability`]."\t".$row[`link`]."\t". $row[`image_link`]."\t".$row[`mpn`]."\t".$row[`brand`]."\t".$row[`google_product_category`]."\t".$row[`Gender`]."\t".$row[`size`]."\t".$row[`Color`]."\n";
} 

fwrite( $fp1,$outPut);
fclose( $fp1 );
于 2013-11-11T09:31:18.437 回答