-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy path3-generic-with-yield.php
40 lines (26 loc) · 1017 Bytes
/
3-generic-with-yield.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
require 'support/bootstrap.php';
use Amp\Future;
use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlConnectionPool;
use function Amp\async;
$db = new MysqlConnectionPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));
$db->query("DROP TABLE IF EXISTS tmp");
/* Create table and insert a few rows */
/* we need to wait until table is finished, so that we can insert. */
$db->query("CREATE TABLE IF NOT EXISTS tmp (a INT(10), b INT(10))");
print "Table successfully created." . PHP_EOL;
$statement = $db->prepare("INSERT INTO tmp (a, b) VALUES (?, ? * 2)");
$future = [];
foreach (\range(1, 5) as $num) {
$future[] = async(fn () => $statement->execute([$num, $num]));
}
/* wait until everything is inserted */
$results = Future\await($future);
print "Insertion successful (if it wasn't, an exception would have been thrown by now)" . PHP_EOL;
$result = $db->query("SELECT a, b FROM tmp");
foreach ($result as $row) {
var_dump($row);
}
$db->query("DROP TABLE tmp");
$db->close();