从 SQL 中提取数据并写入文本文件
我正在尝试从 SQL 中提取数据,然后将其写入文本文件。这在一定程度上做到了这一点,但它只从表中提取 1,该表在文本文件上读取 test:test
。
我希望能够提取所有数据 从表中,然后发布到 列表格式的文本文件,例如 这...
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>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
file_put_contents()
函数会覆盖整个文件 - 这就是为什么你最终只得到每次最后记录。您可以使用
fopen()
和fwrite()
代替。虽然比构建大字符串并使用对
file_put_contents
的单次调用稍微复杂一些,但如果您有大量记录,则不会耗尽内存。The
file_put_contents()
function overwrites the whole file - that's why you end up with only the last record each time.You can use
fopen()
andfwrite()
instead.While a little more complicated than building a large string and using a single call to
file_put_contents
, this won't run out of memory if you have a lot of records.看起来您每次通过 while 循环时都会重新打开并重写文件的全部内容。
试试这个:
另外,如果你不想要 < br >,而是真正的换行符,使用:
It looks like you are reopening and rewriting the entire contents of the file with every pass through your while loop.
Try this:
Also, if you don't want the < br >, but a real line break, use:
其他人已经回答了为什么它只将一项内容写入文件,这更多的是一个建议,但如果您想记录实际的用户和密码,而不是:
$accounts = "$user:$pass
;";
使用
$accounts = $user 。 “:”。 $通行证。 "\n";
但如果您已经知道这一点并且将其用于调试目的,那么请忽略这一点。
祝你好运
Others have answered the why it is only writing one content into the file, this is more of a suggestion but if you want to record the actual user and password, instead of:
$accounts = "$user:$pass<br>";
use
$accounts = $user . ":" . $pass . "\n";
but if you already knew that and were using that for debugging purposes then disregard this.
Good luck
file_put_contents 覆盖现有文件。使用上面的代码,您只会得到一行。
试试这个:
file_put_contents overwrites the existing file. With the above code, you'll only ever get one line.
Try this instead:
您没有将数据库结果附加到 $accounts 字符串;你每次都从头开始创建它。尝试这样的操作:
将其附加到 $accounts 字符串中,然后将所有结果写入 $accounts 字符串中,然后将其写入文件。
You're not appending your database results to the $accounts string; you're creating it from scratch each time. Try something like this:
so you append it, and then once you've got all your results in the $accounts string, you write it out to the file.
如果它是文本文件,则
标记对您也不会特别有用。您需要使用\n
来导致文本文件中出现换行符。如果是html,那么情况就会不同。应该是
,并且您绝对应该将
file_put_contents
拉出循环,否则每次执行循环时都会覆盖该文件。If it's a text file, the
<br>
tag will not be particularly useful to you, as well. You'll need to use\n
to cause a newline to happen in a text file. If it was html, then we'd have a different situation.should be
and you definitely should pull the
file_put_contents
out of the loop or you'll overwrite the file every time you go through the loop.echo "select concat(user,':',pass,'
') from table order by fc desc" | mysql
我更喜欢在 bash 中做这种事情;)
echo "select concat(user,':',pass,'<br>') from table order by fc desc" | mysql <database>
I prefer to do this kind of thing in bash ;)