从 SQL 中提取数据并写入文本文件

发布于 2024-08-10 06:26:03 字数 667 浏览 2 评论 0 原文

我正在尝试从 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>";
?>

I am trying to pull data from SQL, and then write it to a text file. This does that, to an extent, but it only pulls 1 from the table, which reads test:test<br> on the text file.

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

I need to find out what I am doing wrong.

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

提赋 2024-08-17 06:26:03

file_put_contents() 函数会覆盖整个文件 - 这就是为什么你最终只得到每次最后记录。

您可以使用 fopen()fwrite() 代替。

虽然比构建大字符串并使用对 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>";
?>

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() and fwrite() 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.

<?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>";
?>
心凉怎暖 2024-08-17 06:26:03

看起来您每次通过 while 循环时都会重新打开并重写文件的全部内容。

试试这个:

<?php
$sql = mysql_query("SELECT * FROM _$setprofile ORDER BY fc DESC");
$file = "backups/$newcode.txt";
$fh = fopen($file, 'a') or die("can't open file");

while($row = mysql_fetch_array($sql)){
  $user = $row['user'];
  $pass = $row['pass'];

  $accounts = "$user:$pass<br>";

  fwrite($fh, $accounts);
}

fclose($fh);

echo "<a href=backups/$newcode.txt>TEST!</a>";
?>

另外,如果你不想要 < br >,而是真正的换行符,使用:

  $accounts = "$user:$pass\n";

It looks like you are reopening and rewriting the entire contents of the file with every pass through your while loop.

Try this:

<?php
$sql = mysql_query("SELECT * FROM _$setprofile ORDER BY fc DESC");
$file = "backups/$newcode.txt";
$fh = fopen($file, 'a') or die("can't open file");

while($row = mysql_fetch_array($sql)){
  $user = $row['user'];
  $pass = $row['pass'];

  $accounts = "$user:$pass<br>";

  fwrite($fh, $accounts);
}

fclose($fh);

echo "<a href=backups/$newcode.txt>TEST!</a>";
?>

Also, if you don't want the < br >, but a real line break, use:

  $accounts = "$user:$pass\n";
手心的温暖 2024-08-17 06:26:03

其他人已经回答了为什么它只将一项内容写入文件,这更多的是一个建议,但如果您想记录实际的用户和密码,而不是:

$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

在风中等你 2024-08-17 06:26:03

file_put_contents 覆盖现有文件。使用上面的代码,您只会得到一行。

试试这个:

<?php

$sql = mysql_query("SELECT * FROM _$setprofile ORDER BY fc DESC");
$content = "";

while($row = mysql_fetch_array($sql)){
  $user = $row['user'];
  $pass = $row['pass'];

  $accounts = "$user:$pass<br>";
  $content .= $accounts;

}

$file = "backups/$newcode.txt";
file_put_contents($file, $content);

echo "<a href=backups/$newcode.txt>TEST!</a>";
?>

file_put_contents overwrites the existing file. With the above code, you'll only ever get one line.

Try this instead:

<?php

$sql = mysql_query("SELECT * FROM _$setprofile ORDER BY fc DESC");
$content = "";

while($row = mysql_fetch_array($sql)){
  $user = $row['user'];
  $pass = $row['pass'];

  $accounts = "$user:$pass<br>";
  $content .= $accounts;

}

$file = "backups/$newcode.txt";
file_put_contents($file, $content);

echo "<a href=backups/$newcode.txt>TEST!</a>";
?>
离旧人 2024-08-17 06:26:03

您没有将数据库结果附加到 $accounts 字符串;你每次都从头开始创建它。尝试这样的操作:

<?php
$accounts = "";
$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>";
?>

将其附加到 $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:

<?php
$accounts = "";
$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>";
?>

so you append it, and then once you've got all your results in the $accounts string, you write it out to the file.

故事未完 2024-08-17 06:26:03

如果它是文本文件,则
标记对您也不会特别有用。您需要使用 \n 来导致文本文件中出现换行符。如果是html,那么情况就会不同。

$accounts = "$user:$pass<br>";

应该是

$accounts .= "$user:$pass\n";

,并且您绝对应该将 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.

$accounts = "$user:$pass<br>";

should be

$accounts .= "$user:$pass\n";

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.

若言繁花未落 2024-08-17 06:26:03

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文