37

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

4
  • 1
    If you initiate a connection to the db then you MUST know the name of the db Aug 12, 2016 at 23:36
  • 1
    Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here its really pretty easy
    – RiggsFolly
    Aug 12, 2016 at 23:40
  • @RamRaider if you look at OP's code, he is initializing a connection to the server without specifying the schema. He is attempting to list all databases on the server that he as access to.
    – user4914655
    Aug 12, 2016 at 23:42
  • As @RiggsFolly said learn PDO, however the manual might seem a bit overwhelming so you could this, it is extremely good and will have up and running in no time.
    – Script47
    Aug 13, 2016 at 0:31

4 Answers 4

63

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());
8
  • 1
    var_dump is meant for debugging by humans, it's not meant to be parsed by programs.
    – Barmar
    Aug 13, 2016 at 0:30
  • 2
    serialize($myobj) as suggested by @Matth3w is a better solution
    – shogitai
    Apr 4, 2018 at 16:10
  • 5
    The code in the question uses var_dump(). The question was about how to get the output into a file, not the format of the file.
    – Barmar
    Apr 4, 2018 at 18:09
  • 2
    $str = print_r($row, true); when true it returns a string, same with var_dump
    – jscripter
    Mar 25, 2019 at 2:47
  • 2
    Better solution file_put_contents('var_dump.txt',var_export($array, true));
    – AVe
    Mar 23, 2022 at 18:43
25

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.

1
  • 3
    Simple, excellent idea. There is also print_r() to create a string from any variable which may be easier to read than serialized data. Jul 7, 2017 at 0:08
0
<?
$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;
    ?>
-3

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.