php – 从SQL中提取数据,并写入文本文件
作者:互联网
我试图从SQL中提取数据,然后将其写入文本文件.这样做,在某种程度上,但它只从表中读取1,其中读取test:test< br>在文本文件上.
I want to be able to pull all the data
from the table, and then post to the
text file in a list format such as
this…
test:test
test2:test2
test3:test3
我需要找出我做错了什么.
<?php
$sql = mysql_query("SELECT * FROM _$setprofile ORDER BY fc DESC");
while($row = mysql_fetch_array($sql)){
$user = $row['user'];
$pass = $row['pass'];
$accounts = "$user:$pass<br>";
//Functionsss!
$file = "backups/$newcode.txt";
file_put_contents($file, $accounts);
}
echo "<a href=backups/$newcode.txt>TEST!</a>";
?>
解决方法:
file_put_contents()
函数会覆盖整个文件 – 这就是为什么每次只能记录最后一条记录的原因.
虽然比构建一个大字符串并使用一次调用file_put_contents稍微复杂一点,但如果你有很多记录,这将不会耗尽内存.
<?php
$file = "backups/$newcode.txt";
$f = fopen($file, 'w'); // Open in write mode
$sql = mysql_query("SELECT * FROM _$setprofile ORDER BY fc DESC");
while($row = mysql_fetch_array($sql))
{
$user = $row['user'];
$pass = $row['pass'];
$accounts = "$user:$pass<br>";
// Or "$user:$pass\n" as @Benjamin Cox points out
fwrite($f, $accounts);
}
fclose($f);
echo "<a href=backups/$newcode.txt>TEST!</a>";
?>
标签:php,sql,text-files 来源: https://codeday.me/bug/20191007/1864197.html