1. Go to this page and download the library: Download react/mysql 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/ */
react / mysql example snippets
l = new React\Mysql\MysqlClient('user:pass@localhost/bookstore');
$mysql->query('SELECT * FROM book')->then(
function (React\Mysql\MysqlResult $command) {
print_r($command->resultFields);
print_r($command->resultRows);
echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
},
function (Exception $error) {
echo 'Error: ' . $error->getMessage() . PHP_EOL;
}
);
$mysql = new React\Mysql\MysqlClient($uri);
$mysql->query(…);
$mysql = new React\Mysql\MysqlClient('user:secret@localhost:3306/database');
$mysql->query('CREATE TABLE test ...');
$mysql->query('INSERT INTO test (id) VALUES (1)');
$mysql->query($query)->then(function (React\Mysql\MysqlResult $command) {
if (isset($command->resultRows)) {
// this is a response 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 (Exception $error) {
// the query was not executed successfully
echo 'Error: ' . $error->getMessage() . PHP_EOL;
});
$mysql->query('SELECT * FROM user WHERE id > ?', [$id]);
$stream = $mysql->queryStream('SELECT * FROM user');
$stream->on('data', function ($row) {
echo $row['name'] . PHP_EOL;
});
$stream->on('end', function () {
echo 'Completed.';
});
$stream = $mysql->queryStream('SELECT * FROM user WHERE id > ?', [$id]);
$mysql->queryStream('SELECT * FROM user')->pipe($formatter)->pipe($logger);