代码之家  ›  专栏  ›  技术社区  ›  homework

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

  •  1
  • homework  · 技术社区  · 15 年前

    我试图从SQL中提取数据,然后将其写入文本文件。这在某种程度上是这样的, test:test<br>

    我希望能够提取所有数据 列表格式的文本文件,例如

        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>";
    ?>
    
    7 回复  |  直到 15 年前
        1
  •  5
  •   Greg    15 年前

    这个 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>";
    ?>
    
        2
  •  3
  •   Benjamin Cox    15 年前

    每次通过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";
    
        3
  •  2
  •   Anthony Forloney    15 年前

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

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

    使用

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

    但是,如果您已经知道这一点,并将其用于调试目的,那么忽略这一点。

    祝你好运

        4
  •  1
  •   Phil Hayward    15 年前

    <?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>";
    ?>
    
        5
  •  1
  •   richsage    15 年前

    您没有将数据库结果附加到$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字符串中获得所有结果后,将其写入文件。

        6
  •  1
  •   Paul K    15 年前

    如果是文本文件,则 <br> 标签也不会对您特别有用。你需要使用 \n 使换行符出现在文本文件中。如果是html,那么情况就不同了。

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

    应该是

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

    而且你绝对应该把车开走 file_put_contents

        7
  •  0
  •   RusHughes    15 年前

    echo "select concat(user,':',pass,'<br>') from table order by fc desc" | mysql <database>

    我更喜欢在bash中做这种事情;)