将大型mysql输出加载到变量中以写入文件

时间:2009-08-04 18:48:53

标签: php mysql

这是我必须编写文件的代码

<?
$date = date(F jS, Y g:i:s a);
$filename = "$date.csv";
$fh = fopen($filename, 'w') or die("can't open file");
$data = "";
fwrite($fh, $data);
fclose($fh);
?>

我需要编写的数据包含mysql查询的输出,如下所示

echo "<tr>";  
echo "<td valign='top'>" . nl2br( $row['productid']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['user_id']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['category_id']) . "</td>";  
echo "<td valign='top'><input type=text value=\"" . nl2br( $row['producttitle']) . "\"></td>";  
echo "<td valign='top'>" . nl2br( $row['item_counter_style']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['Quantity']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['payment_gateway']) . "</td>";  
echo "<td valign='top'><div id=ddesc>" . nl2br( $row['detailed_descrip']) . "</div></td>";  
echo "<td valign='top'>" . nl2br( $row['selling_method']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['min_bid_amount']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['bidincrement']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['shipping_cost']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['who_pay_shipping']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['shipping_route']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['duration']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['currency']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['reserve_price']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['quick_buy_price']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['bid_starting_date']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['start_delay']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['status']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture1']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture2']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture3']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture4']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture5']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture6']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture7']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['picture8']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['payment_name']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['quantity_sold']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['clicks']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['expire_date']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['payment_id']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['size_of_quantity']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['tax']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['sniper']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['no_of_repost']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['sale_price']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['s_repost']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['shop']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['relisted']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['or_price']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['return_pol']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['timetoadd']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['special']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['discount']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['bonus']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['totbid']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['freebids']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['autobidder']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['supplier']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['supplier_cost']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['supplier_item_number']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['supplier_shipping_cost']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['supplier_notes']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['shipping_cost_a']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['shipping_cost_c']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row['Auction_ID']) . "</td>";  
echo "<td valign='top'><a href=edit.php?productid={$row['productid']}>Edit</a></td><td><a href=delete.php?productid={$row['productid']}>Delete</a></td> "; 
echo "<td><input type=submit class=clone value=clone></td></tr>\r\n";
echo "</tr>"; 

如何将所有这些(上图)纳入$ data,写入我的文件?

1 个答案:

答案 0 :(得分:2)

将所有echo替换为$data .=,你应该好好去。 (当然,您需要在之后放置fwrite()fclose()来电

<?
$date = date(F jS, Y g:i:s a);
$filename = "$date.csv";
$fh = fopen($filename, 'w') or die("can't open file");
$data = "";

$data .= "<tr>";  
$data .= "<td valign='top'>" . nl2br( $row['productid']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['user_id']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['category_id']) . "</td>";  
$data .= "<td valign='top'><input type=text value=\"" . nl2br( $row['producttitle']) . "\"></td>";  
$data .= "<td valign='top'>" . nl2br( $row['item_counter_style']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['Quantity']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['payment_gateway']) . "</td>";  
$data .= "<td valign='top'><div id=ddesc>" . nl2br( $row['detailed_descrip']) . "</div></td>";  
$data .= "<td valign='top'>" . nl2br( $row['selling_method']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['min_bid_amount']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['bidincrement']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['shipping_cost']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['who_pay_shipping']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['shipping_route']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['duration']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['currency']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['reserve_price']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['quick_buy_price']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['bid_starting_date']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['start_delay']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['status']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture1']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture2']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture3']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture4']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture5']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture6']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture7']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['picture8']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['payment_name']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['quantity_sold']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['clicks']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['expire_date']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['payment_id']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['size_of_quantity']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['tax']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['sniper']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['no_of_repost']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['sale_price']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['s_repost']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['shop']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['relisted']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['or_price']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['return_pol']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['timetoadd']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['special']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['discount']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['bonus']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['totbid']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['freebids']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['autobidder']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['supplier']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['supplier_cost']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['supplier_item_number']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['supplier_shipping_cost']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['supplier_notes']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['shipping_cost_a']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['shipping_cost_c']) . "</td>";  
$data .= "<td valign='top'>" . nl2br( $row['Auction_ID']) . "</td>";  
$data .= "<td valign='top'><a href=edit.php?productid={$row['productid']}>Edit</a></td><td><a href=delete.php?productid={$row['productid']}>Delete</a></td> "; 
$data .= "<td><input type=submit class=clone value=clone></td></tr>\r\n";
$data .= "</tr>";

fwrite($fh, $data);
fclose($fh);
?>