save var_dump into text file for multi server ip from text file

左心房为你撑大大i 提交于 2020-01-03 04:51:08

问题


this php code for sql query, if I have multi sql sever into text file and I want get servers from this file. how I can save var_dump for each sever into "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]: failed to open stream: Invalid argument in C:\AppServ\www\connectdb.php on line 24


回答1:


Perhaps something more precise, like print_r would work, where it has a mode for storing the output

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



回答2:


I think instead of

ob_get_flush()

You want

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

To put it all togather you would need to do this

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 current buffer contents and delete current output buffer ... Returns the contents of the output buffer and end output buffering. If output buffering isn't active then FALSE is returned.

-Note- the end output buffering this is why we need to restart it ob_start() on each iteration of the loop.

For ob_get_flush()

The output buffer must be started by ob_start() with PHP_OUTPUT_HANDLER_FLUSHABLE flag. Otherwise ob_get_flush() will not work.

That said it may not be the best approach for your goal. As an alternative method, you can use this function

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 reversed the Parameters for array map. This is from an Exception/Error handling class I just wrote, you can modify it to more closely match var_dump if you want. Currently, it's setup to match the exception stack trace printout.

By the way if you want a really killer debuggin class, you can use the one from my framework

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

With some small modification it may fit your needs ( perhaps a bit overkill )

This is what it outputs

$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 only post the link because it's not trivial to deal with all the possible variable types, and it doesn't help here that PHP is Non-Typed language.



来源:https://stackoverflow.com/questions/38928140/save-var-dump-into-text-file-for-multi-server-ip-from-text-file

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