此代码不起作用我想为每 10 条记录打印文件。if($con=mysqli_connect("localhost","root","","fileDemo")){ //echo "DB CONEECTED"; $sql = "select id from news order by id desc"; $data_query=mysqli_query($con,$sql); while($data=mysqli_fetch_assoc($data_query)){ $id=$data['id']; if(!file_exists("demo_file"."-".trim($id).".txt")){ $file_c=fopen("demo_file"."-".trim($id).".txt", 'a'); fwrite($file_c,$id); fclose($file_c); } // $stringData.="Name: ".$data['name']." Age:".$data['age']."\n"; }}else{ echo "CONNECTION FAILD";}
1 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
//open first file
$handle = fopen('filename.txt', 'w');
foreach ($records as $i => $record) {
//if record is 10th then close prev file and open new
if ($i % 10 == 0) {
fclose($handle);
$handle = fopen("filename_$i.txt", 'w');
}
//your logic here. Write to file I suppose
fwrite($handle, $record);
}
//close file
fclose($handle);
- 1 回答
- 0 关注
- 204 浏览
添加回答
举报
0/150
提交
取消