php 将 var_dump 保存到文本文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38927628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:46:01  来源:igfitidea点击:

save var_dump into text file

phpmysqlsqlvar-dump

提问by Yasser Abo Reida

I have the php code for sql query

我有用于 sql 查询的 php 代码

<?
$server = "127.0.0.1";
$username = "root";
$password = "1";

$link= connecttodb($server,$username,$password);

function connecttodb($server,$username,$password)
{

    $rez=fopen("test.txt","ab");
    if ($link=mysql_connect ("$server","$username","$password",TRUE))
    {
        fwrite($rez,"".$server." \r\n");
     echo "Connected successfully to >> " .$server ;
  
  $result = mysql_query('SHOW DATABASES');
        echo "<br>";
        while ($row = mysql_fetch_array($result))
        {
            var_dump ($row); }
     }
    }
    ini_set('max_execution_time', 10);
    return $link;
?>

this code print my database name on the browser how I can save the database name into text file

此代码在浏览器上打印我的数据库名称如何将数据库名称保存到文本文件中

Connected successfully to >> 127.0.0.1
array(2) { [0]=> string(18) "information_schema" ["Database"]=> string(18) "information_schema" } array(2) { [0]=> string(2) "db" ["Database"]=> string(2) "db" } array(2) { [0]=> string(5) "mysql" ["Database"]=> string(5) "mysql" } array(2) { [0]=> string(10) "phpmyadmin" ["Database"]=> string(10) "phpmyadmin" } array(2) { [0]=> string(4) "test" ["Database"]=> string(4) "test" }

回答by Barmar

You can use the output buffering functions to capture output and write it to a file.

您可以使用输出缓冲函数来捕获输出并将其写入文件。

ob_flush();
ob_start();
while ($row = mysql_fetch_assoc($result)) {
    var_dump($row);
}
file_put_contents("dump.txt", ob_get_flush());

回答by Yasser Abo Reida

Don't use var_dump for this, use serialize like so:

不要为此使用 var_dump,像这样使用序列化:

<?php
$fp = fopen('vardump.txt', 'w');
fwrite($fp, serialize($myobj));
fclose($fp);
?>

To restore it, you can use unserialize($filecontents); by reading it back in from the file.

要恢复它,您可以使用 unserialize($filecontents); 通过从文件中读回它。

回答by Yasser Abo Reida

<?
$server = "127.0.0.1";
$username = "root";
$password = "1";

$link= connecttodb($server,$username,$password);

function connecttodb($server,$username,$password)
{

$rez=fopen("test.txt","ab");
   if ($link=mysql_connect ("$server","$username","$password",TRUE))
   {
   fwrite($rez,"".$server." \r\n");
    echo "Connected successfully to >> " .$server ;

        $result = mysql_query('SHOW DATABASES');
echo "<br>";
while ($row = mysql_fetch_array($result))
{
fwrite($rez, $row['DatabaseName']); }

    }

}
ini_set('max_execution_time', 10);
return $link;
    ?>

回答by Andy

This should work

这应该工作

$file = 'somefile.txt';
file_put_contents($file, $some_var);

You may want to serialize the variable first to make it more readable.

您可能希望先序列化变量以使其更具可读性。

But there are several other methods: http://php.net/manual/en/function.file-put-contents.php

但还有其他几种方法:http: //php.net/manual/en/function.file-put-contents.php