コード例 #1
0
ファイル: 001_connect.php プロジェクト: amphp/mysql
<?php

/*
 * Generic example for establishing a connection
 */
require './example_bootstrap.php';
\Amp\run(function () {
    /* If you want ssl, pass as second argument an array with ssl options (an empty options array is valid too); if null is passed, ssl is not enabled either */
    $db = new \Amp\Mysql\Pool("host=" . DB_HOST . ";user="******";pass="******";db=" . DB_NAME);
    /* use an alternative charset... Default is utf8mb4_general_ci */
    $db->setCharset("latin1_general_ci");
    /* do something with your connection(s) maintained by Pool */
    /* we always close the database here so that there is no read/write watcher anymore and Reactor terminates itself */
    $db->close();
});
コード例 #2
0
<?php

require './example_bootstrap.php';
\Amp\run(function () {
    $db = new \Amp\Mysql\Pool("host=" . DB_HOST . ";user="******";pass="******";db=" . DB_NAME);
    /* Create table and insert a few rows */
    /* we need to wait until table is finished, so that we can insert. */
    (yield $db->query("CREATE TABLE IF NOT EXISTS tmp SELECT 1 AS a, 2 AS b"));
    $promises = [];
    foreach (range(1, 5) as $num) {
        $promises[] = $db->query("INSERT INTO tmp (a, b) VALUES ({$num}, {$num} * 2)");
    }
    /* wait until everything is inserted (in case where we wouldn't have to wait, we also could just  */
    (yield \Amp\all($promises));
    print "Insertion successful (if it wasn't, an exception would have been thrown by now)\n";
    $db->close();
});
コード例 #3
0
ファイル: 002_simple_query.php プロジェクト: amphp/mysql
<?php

require './example_bootstrap.php';
\Amp\run(function () {
    $db = new \Amp\Mysql\Pool("host=" . DB_HOST . ";user="******";pass="******";db=" . DB_NAME);
    /* yeah, we need a lot of yields and assigns here... With PHP 7 we finally can drop a lot of stupid parenthesis! */
    $query = (yield $db->query("SELECT 1"));
    list($one) = (yield $query->fetchRow());
    var_dump($one);
    // should output string(1) "1"
    $db->close();
});