Skip to content

Commit a0ef8c9

Browse files
committed
Update examples
1 parent 3d5ca3b commit a0ef8c9

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

examples/2-simple-query.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
require 'support/bootstrap.php';
44

5+
use Amp\Mysql\ResultSet;
6+
57
Amp\Loop::run(function() {
68
$db = Amp\Mysql\pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
79

8-
$query = yield $db->query("SELECT 1");
9-
list($one) = yield $query->fetchRow();
10+
/** @var \Amp\Mysql\ResultSet $result */
11+
$result = yield $db->query("SELECT 1 AS value");
1012

11-
var_dump($one); // should output string(1) "1"
13+
while (yield $result->advance(ResultSet::FETCH_ARRAY)) {
14+
$row = $result->getCurrent();
15+
var_dump($row[0]);
16+
}
1217

1318
$db->close();
1419
});

examples/3-generic-with-yield.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
/* Create table and insert a few rows */
99
/* we need to wait until table is finished, so that we can insert. */
10-
yield $db->query("CREATE TABLE IF NOT EXISTS tmp SELECT 1 AS a, 2 AS b");
10+
yield $db->query("CREATE TABLE IF NOT EXISTS tmp (a INT(10), b INT(10))");
1111

1212
print "Table successfully created." . PHP_EOL;
1313

14+
/** @var \Amp\Mysql\Statement $statement */
1415
$statement = yield $db->prepare("INSERT INTO tmp (a, b) VALUES (?, ? * 2)");
1516

1617
$promises = [];
@@ -23,6 +24,13 @@
2324

2425
print "Insertion successful (if it wasn't, an exception would have been thrown by now)" . PHP_EOL;
2526

27+
/** @var \Amp\Mysql\ResultSet $result */
28+
$result = yield $db->execute("SELECT a, b FROM tmp");
29+
30+
while (yield $result->advance()) {
31+
var_dump($result->getCurrent());
32+
}
33+
2634
yield $db->query("DROP TABLE tmp");
2735

2836
$db->close();

examples/4-multi-rows.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@
1111

1212
$promises = [];
1313

14-
/* yeah, we need a lot of yields and assigns here... */
1514
$promises[] = $db->query("SELECT a * b FROM tmp");
15+
$promises[] = $db->execute("SELECT POW(a, ?) AS power FROM tmp", 2);
1616

17-
/* or, maybe, wait until they're all fetched (because you anyway only can continue after having full resultset */
18-
$promises[] = $db->query("SELECT a * b FROM tmp");
19-
20-
list($result1, $result2) = yield $promises;
17+
/**
18+
* @var \Amp\Mysql\ResultSet $result1
19+
* @var \Amp\Mysql\ResultSet $result2
20+
*/
21+
list($result1, $result2) = yield $promises; // Both queries execute simultaneously. Wait for both to finish here.
2122

23+
print "Query 1 Results:" . PHP_EOL;
2224
while (yield $result1->advance()) {
2325
var_dump($result1->getCurrent());
2426
}
2527

28+
print PHP_EOL . "Query 2 Results:" . PHP_EOL;
29+
while (yield $result2->advance()) {
30+
var_dump($result2->getCurrent());
31+
}
32+
2633
yield $db->query("DROP TABLE tmp");
2734

2835
$db->close();

examples/5-multi-stmts.php

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
var_dump($result->getCurrent());
1818
}
1919

20-
var_dump(yield $result->getFields());
21-
2220
yield $db->query("DROP TABLE tmp");
2321

2422
$db->close();

examples/support/generic-table.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
/* Create table and fill in a few rows for examples; for comments see 3-generic-with-yield.php */
44
function createGenericTable(\Amp\Mysql\Link $db): Generator {
5-
yield $db->query("CREATE TABLE IF NOT EXISTS tmp SELECT 1 AS a, 2 AS b");
5+
yield $db->query("DROP TABLE IF EXISTS tmp");
6+
7+
yield $db->query("CREATE TABLE tmp (a INT(10), b INT(10))");
68

79
$statement = yield $db->prepare("INSERT INTO tmp (a, b) VALUES (?, ? * 2)");
810

0 commit comments

Comments
 (0)