save var_dump into text file [closed]

瘦欲@ 提交于 2019-12-04 16:50:28

问题


I have the php code for sql query

<?
$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" }

回答1:


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



回答2:


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

<?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.




回答3:


<?
$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;
    ?>



回答4:


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



来源:https://stackoverflow.com/questions/38927628/save-var-dump-into-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!