为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用php基于多条记录创建多个文件假设我有100条记录为每10条记录创建1个文件

如何使用php基于多条记录创建多个文件假设我有100条记录为每10条记录创建1个文件

PHP
婷婷同学_ 2021-07-13 16:02:04
此代码不起作用我想为每 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);


查看完整回答
反对 回复 2021-07-16
  • 1 回答
  • 0 关注
  • 204 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信