PHP将800条记录从数据库写入文件(.txt) [英] PHP writing 800 records to file(.txt) from database
本文介绍了PHP将800条记录从数据库写入文件(.txt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从30列以上的8000多个数据库中访问记录,并写入由制表符分隔的文本文件. 我的php代码是:
I am trying to access records from database more than 8000 with 30 columns and write to text file separated by tab. My php code is :
$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%的使用率.
But it taking too long time to execute.How I can improve performance of code? I can not utilize more than 25% usage on my server.
推荐答案
构造要在变量中写入的数据,只需对文件进行一次写入.其效率更高.
construct the data u want to write in a variable and just do one write to file. its much more efficient.
$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 );
这篇关于PHP将800条记录从数据库写入文件(.txt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文