php - 一段时间后 file_put_contents

标签 php mysql foreach while-loop

是否可以将 file_put_contents() 函数放入 while() 循环中?

我正在尝试让我的脚本将行写入文本文件 - 有超过 4000 行要写入。

$glassquery = $db->query("SELECT item FROM glass");

while($glass = $glassquery->fetch_assoc()) {
    file_put_contents('download.txt', $glass['item'] . PHP_EOL);
}

因此,它将项目名称写入文本文件,然后在新行中写入下一个项目名称。

最佳答案

我认为您想要的是 FILE_APPEND 标志:

while($glass = $glassquery->fetch_assoc()) {
    file_put_contents('download.txt', $glass['item'] . PHP_EOL, FILE_APPEND);
}

这将一次写入一行,而不删除前面的行。

关于php - 一段时间后 file_put_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26944756/

相关文章:

php - 使用 PHP 从 mySQL 数据库提交表单生成电子邮件

php - Symfony 3 bcrypt 密码不验证

php - 遍历 $_POST 数组 item_nameX(动态添加数字到变量名称的末尾)

mysql - SQL 连接 : counting rows and get most recent row from another table

在 foreach 循环中运行 h2o 算法?

r - 为什么这个 foreach/dopar 函数没有使用我要求的所有内核?

php - 如何用 PHP 解析大的 JSON 文件

php - MySQL ORDER BY 和日期字段具有不可预测的结果

mysql - 无法在Raspberry Pi上下载MySQL软件

php - SQL - 过滤搜索结果