forked from amphp/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6-transaction.php
36 lines (24 loc) · 1.06 KB
/
6-transaction.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
<?php
require 'support/bootstrap.php';
require 'support/generic-table.php';
use Amp\Mysql;
Amp\Loop::run(function () {
$db = Mysql\pool(Mysql\ConnectionConfig::fromString("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME));
/* create same table than in 3-generic-with-yield.php */
yield from createGenericTable($db);
/** @var \Amp\Sql\Transaction $transaction */
$transaction = yield $db->beginTransaction();
yield $transaction->execute("INSERT INTO tmp VALUES (?, ? * 2)", [6, 6]);
/** @var Mysql\ResultSet $result */
$result = yield $transaction->execute("SELECT * FROM tmp WHERE a >= ?", [5]); // Two rows should be returned.
while (yield $result->advance()) {
\var_dump($result->getCurrent());
}
yield $transaction->rollback();
// Run same query again, should only return a single row since the other was rolled back.
$result = yield $db->execute("SELECT * FROM tmp WHERE a >= ?", [5]);
while (yield $result->advance()) {
\var_dump($result->getCurrent());
}
$db->close();
});