Skip to content

Commit e48ed96

Browse files
authored
Merge pull request #45 from clue-labs/example
Add quickstart example and interactive CLI example
2 parents d1ab545 + 7781181 commit e48ed96

File tree

4 files changed

+183
-28
lines changed

4 files changed

+183
-28
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,49 @@ It implements the MySQL protocol and allows you to access your existing MySQL
99
database.
1010
It is written in pure PHP and does not require any extensions.
1111

12+
**Table of contents**
13+
14+
* [Quickstart example](#quickstart-example)
15+
* [Usage](#usage)
16+
* [Connection](#connection)
17+
* [connect()](#connect)
18+
* [Install](#install)
19+
* [Tests](#tests)
20+
* [License](#license)
21+
22+
## Quickstart example
23+
24+
This example runs a simple `SELECT` query and dumps all the records from a `book` table:
25+
26+
```php
27+
$loop = React\EventLoop\Factory::create();
28+
29+
$connection = new React\MySQL\Connection($loop, array(
30+
'dbname' => 'test',
31+
'user' => 'test',
32+
'passwd' => 'test',
33+
));
34+
35+
$connection->connect(function () {});
36+
37+
$connection->query('SELECT * FROM book', function (QueryCommand $command) {
38+
if ($command->hasError()) {
39+
$error = $command->getError();
40+
echo 'Error: ' . $error->getMessage() . PHP_EOL;
41+
} else {
42+
print_r($command->resultFields);
43+
print_r($command->resultRows);
44+
echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
45+
}
46+
});
47+
48+
$connection->close();
49+
50+
$loop->run();
51+
```
52+
53+
See also the [examples](examples).
54+
1255
## Usage
1356

1457
### Connection

examples/01-query.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use React\MySQL\Commands\QueryCommand;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
//create the main loop
8+
$loop = React\EventLoop\Factory::create();
9+
10+
//create a mysql connection for executing queries
11+
$connection = new React\MySQL\Connection($loop, array(
12+
'dbname' => 'test',
13+
'user' => 'test',
14+
'passwd' => 'test',
15+
));
16+
17+
$connection->connect(function () {});
18+
19+
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
20+
$connection->query($query, function (QueryCommand $command) {
21+
if ($command->hasError()) {
22+
// test whether the query was executed successfully
23+
// get the error object, instance of Exception.
24+
$error = $command->getError();
25+
echo 'Error: ' . $error->getMessage() . PHP_EOL;
26+
} elseif (isset($command->resultRows)) {
27+
// this is a response to a SELECT etc. with some rows (0+)
28+
print_r($command->resultFields);
29+
print_r($command->resultRows);
30+
echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
31+
} else {
32+
// this is an OK message in response to an UPDATE etc.
33+
if ($command->insertId !== 0) {
34+
var_dump('last insert ID', $command->insertId);
35+
}
36+
echo 'Query OK, ' . $command->affectedRows . ' row(s) affected' . PHP_EOL;
37+
}
38+
});
39+
40+
$connection->close();
41+
42+
$loop->run();

examples/11-interactive.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
use React\MySQL\Commands\QueryCommand;
4+
use React\Stream\ReadableResourceStream;
5+
use React\MySQL\ConnectionInterface;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
//create the main loop
10+
$loop = React\EventLoop\Factory::create();
11+
12+
// open a STDIN stream to read keyboard input (not supported on Windows)
13+
$stdin = new ReadableResourceStream(STDIN, $loop);
14+
15+
//create a mysql connection for executing queries
16+
$connection = new React\MySQL\Connection($loop, array(
17+
'dbname' => 'test',
18+
'user' => 'test',
19+
'passwd' => 'test',
20+
));
21+
22+
$connection->connect(function ($e) use ($stdin) {
23+
if ($e === null) {
24+
echo 'Connection success.' . PHP_EOL;
25+
} else {
26+
echo 'Connection error: ' . $e->getMessage() . PHP_EOL;
27+
$stdin->close();
28+
}
29+
});
30+
31+
$stdin->on('data', function ($line) use ($connection) {
32+
$query = trim($line);
33+
34+
if ($query === '') {
35+
// skip empty commands
36+
return;
37+
}
38+
if ($query === 'exit') {
39+
// exit command should close the connection
40+
echo 'bye.' . PHP_EOL;
41+
$connection->close();
42+
return;
43+
}
44+
45+
$time = microtime(true);
46+
$connection->query($query, function (QueryCommand $command) use ($time) {
47+
if ($command->hasError()) {
48+
// test whether the query was executed successfully
49+
// get the error object, instance of Exception.
50+
$error = $command->getError();
51+
echo 'Error: ' . $error->getMessage() . PHP_EOL;
52+
} elseif (isset($command->resultRows)) {
53+
// this is a response to a SELECT etc. with some rows (0+)
54+
echo implode("\t", array_column($command->resultFields, 'name')) . PHP_EOL;
55+
56+
foreach ($command->resultRows as $row) {
57+
echo implode("\t", $row) . PHP_EOL;
58+
}
59+
60+
printf(
61+
'%d row%s in set (%.03f sec)%s',
62+
count($command->resultRows),
63+
count($command->resultRows) === 1 ? '' : 's',
64+
microtime(true) - $time,
65+
PHP_EOL
66+
);
67+
} else {
68+
// this is an OK message in response to an UPDATE etc.
69+
// the insertId will only be set if this is
70+
if ($command->insertId !== 0) {
71+
var_dump('last insert ID', $command->insertId);
72+
}
73+
74+
printf(
75+
'Query OK, %d row%s affected (%.03f sec)%s',
76+
$command->affectedRows,
77+
$command->affectedRows === 1 ? '' : 's',
78+
microtime(true) - $time,
79+
PHP_EOL
80+
);
81+
}
82+
});
83+
});
84+
85+
// close connection when STDIN closes (EOF or CTRL+D)
86+
$stdin->on('close', function () use ($connection) {
87+
if ($connection->getState() === ConnectionInterface::STATE_AUTHENTICATED) {
88+
$connection->close();
89+
}
90+
});
91+
92+
// close STDIN (stop reading) when connection closes
93+
$connection->on('close', function () use ($stdin) {
94+
$stdin->close();
95+
echo 'Disconnected.' . PHP_EOL;
96+
});
97+
98+
$loop->run();

examples/query-with-callback.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)