PHP code example of wpjscc / reactphp-mysql-pool-server

1. Go to this page and download the library: Download wpjscc/reactphp-mysql-pool-server library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wpjscc / reactphp-mysql-pool-server example snippets




use Wpjscc\MySQLServer\Server;
use Wpjscc\MySQL\Pool;


new Server(3308, new Pool(
    getenv('MYSQL_URI') ?: 'username:password@host/databasename?timeout=5',
    [
        'min_connections' => 2, // 10 connection
        'max_connections' => 10, // 10 connection
        'max_wait_queue' => 70, // how many sql in queue
        'wait_timeout' => 5, // wait time 



use Wpjscc\MySQL\Pool;
use Wpjscc\MySQLServer\Factory;
use React\MySQL\QueryResult;
use React\EventLoop\Loop;

$pool = new Pool(
    '127.0.0.1:3308',
    [
        'min_connections' => 2, // 10 connection
        'max_connections' => 10, // 10 connection
        'max_wait_queue' => 70, // how many sql in queue
        'wait_timeout' => 5,// wait time  to a SELECT etc. with some rows (0+)
                // print_r($command->resultFields);
                // print_r($command->resultRows);
                echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
            } else {
                // this is an OK message in response to an UPDATE etc.
                if ($command->insertId !== 0) {
                    var_dump('last insert ID', $command->insertId);
                }
                echo 'Query OK, ' . $command->affectedRows . ' row(s) affected' . PHP_EOL;
            }
        }, function ($error) {
            // the query was not executed successfully
            echo 'Error: ' . $error->getMessage() . PHP_EOL;
        });
        
    }
}


function queryStream($pool){
    for ($i=0; $i < 90; $i++) { 
        (function($pool,$i){
            $stream = $pool->queryStream('select * from blog');
           
            $stream->on('data', function ($data) use ($i) {
                // echo "queryStream:$i\n";
                // print_r($data);
            });
            $stream->on('error', function ($err) {
                echo 'Error: ' . $err->getMessage() . PHP_EOL;
            });
            $stream->on('end', function () use ($i) {
                echo 'Completed.'.$i . PHP_EOL;
            });
            
           
        })($pool, $i);
        
    }
}

$pool->translation(function ($connection) {
    React\Async\await($connection->query("INSERT INTO blog_test (content) VALUES ('hello world success')"));
    return \React\Promise\resolve('hello world');
})->then(function($result){
    var_dump($result);
}, function($error){
    var_dump($error->getMessage());
});