Save var_dump to text file for multi server ip from text file

this php code for sql query if i have multi sql sever to text file and i want to get servers from this file. how can i save var_dump for each split on "serverip.txt"

<? $list =file('servers.txt'); $username = "root"; $password = "1"; foreach($list as $server) $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>"; ob_flush(); ob_start(); while ($row = mysql_fetch_assoc($result)) { var_dump($row); file_put_contents("$server.txt", ob_get_flush()); } } } ini_set('max_execution_time', 10); return $link; ?> 

I have this error

Warning: file_put_contents (ServerIp.txt) [function.file-put-contents]: could not open stream: invalid argument in C: \ AppServ \ www \ connectdb.php on line 24

0
sql php mysql var-dump
source share
2 answers

Perhaps something more accurate, for example, print_r will work, where it has a mode for storing output

 while ($row = mysql_fetch_assoc($result)) { file_put_contents("$server.txt", print_r($row, true)); } 
+1
source share

I think instead

 ob_get_flush() 

Do you want to

 $var = ob_get_clean(); file_put_contents("$server.txt", $var ); 

To do all this, you will need to do it

 while ($row = mysql_fetch_assoc($result)) { ob_start(); var_dump($row); file_put_contents("$server.txt",ob_get_clean()); } 

http://php.net/manual/en/function.ob-get-clean.php

Get the current contents of the buffer and delete the current output buffer ... Returns the contents of the output buffer and the end of output buffering. If output buffering is not active, FALSE is returned.

-Note- the end output buffering That's why we need to restart its ob_start() on each iteration of the loop.

For ob_get_flush()

The output buffer should be started using ob_start () with the PHP_OUTPUT_HANDLER_FLUSHABLE flag. Otherwise, ob_get_flush () will not work.

This suggests that this is not the best approach to your goal. As an alternative method, you can use this funciton

 function getVarType( $var, $escapeHtml = true ){ $strArg = 'unknown'; switch ( gettype( $var ) ){ case 'boolean': return ( $var ? 'true' : 'false' ); case 'integer': return intval( $var ); case 'double': return floatval( $var ); case 'string': if( $escapeHtml ){ $var = htmlentities( $var, ENT_NOQUOTES, 'UTF-8', false); } return "'".$var."'"; case 'resource': return 'Resource id #'.intval( $var ); case 'NULL': return 'NULL'; case 'array': return "Array"; case 'object': return 'Object('.get_class( $var ).')'; case 'unknown type': default: return'UNKNOWN TYPE'; } } $row = array_map( 'getVarType', $row ); 

However, I think in PHP 7 they canceled the options for an array map. This is from the exception / error handling class that I just wrote, you can modify it to match var_dump more precisely if you want. It is currently configured to match the printout of the exception stack trace.

By the way, if you really want the killer debugger class, you can use it from my framework

https://github.com/ArtisticPhoenix/Evo/blob/master/EVO/Debug.php

With some slight modification, it may suit your needs (maybe outsmart a little)

This is what it infers

 $G = ['one'=>1, 'two' => 2, 3=>['foo', 'bar']]; EVO_Debug::dump( $G ); ==================================== EVO_DEBUG::DUMP ===================================== Output from FILE[ C:\UniServerZ\www\Evo\EVO\bootstrap.php ] on LINE[ 72 ] ------------------------------------------------------------------------------------------ array(3){ ["one"] => int(1), ["two"] => int(2), [3] => array(2){ [0] => string(3) "foo", [1] => string(3) "bar", }, } ========================================================================================== 

Although you want to use

  EVO_Debug::export( $G ); 

It can also output private properties and class constants. I send the link only because it is not trivial to deal with all possible types of variables, and here it does not help that PHP is a language without typing.

0
source share

All Articles