Swoole Coroutine MySQL

Swoole\Coroutine\MySQL

Coroutine MySQL client.

Example:

<?php
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
    'host' => '127.0.0.1',
    'port' => 3306,
    'user' => 'user',
    'password' => 'pass',
    'database' => 'test',
]);
$res = $swoole_mysql->query('select sleep(1)');
bool Swoole\Coroutine\MySQL->connect(array $serverInfo)

Establish the connection to MySQL server.

$serverInfo:

<?php
[
    'host' => 'MySQL server IP',
    'user' => 'username',
    'password' => 'password',
    'database' => 'DB name',
    'port'    => 'DB port',
    'timeout' => 'Timeout',
    'charset' => 'charset',
    'strict_type' => false, 
    'fetch_mode' => true,
]
Swoole\Coroutine\MySQL->query(string $sql, double $timeout = -1) : array|bool

Execute MySQL query.

Example:

<?php
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
    'host' => '127.0.0.1',
    'port' => 3306,
    'user' => 'user',
    'password' => 'pass',
    'database' => 'test',
]);
$res = $swoole_mysql->query('select * from $table');
if($res === false) {
    return;
}
foreach ($res as $value) {
    echo $value['f_filed_name'];
}
Swoole\Coroutine\MySQL->prepare(string $sql, float $timeout) : bool

Similar to prepare in MySQL client.

Example:

<?php
use Swoole\Coroutine as co;
co::create(function() {
    $db = new co\MySQL();
    $server = array(
        'host' => '127.0.0.1',
        'user' => 'root',
        'password' => 'root',
        'database' => 'test',
    );

    $ret1 = $db->connect($server);
    $stmt = $db->prepare('SELECT * FROM userinfo WHERE id=?');
    if ($stmt == false)
    {
        var_dump($db->errno, $db->error);
    }
    else
    {
        $ret2 = $stmt->execute(array(10));
        var_dump($ret2);
    }
});
Swoole\Coroutine\MySQL\Statement->execute(array $params, float $timeout = -1) : bool

Similar to execute in MySQL client.

<?php
use Swoole\Coroutine as co;
co::create(function() {
    $db = new co\MySQL();
    $server = array(
        'host' => '127.0.0.1',
        'user' => 'root',
        'password' => 'root',
        'database' => 'test',
    );

    $ret1 = $db->connect($server);
    $stmt = $db->prepare('SELECT * FROM userinfo WHERE id=? and name=?');
    if ($stmt == false)
    {
        var_dump($db->errno, $db->error);
    }
    else
    {
        $ret2 = $stmt->execute(array(10, 'rango'));
        var_dump($ret2);

        $ret3 = $stmt->execute(array(13, 'alvin'));
        var_dump($ret3);
    }
});
Swoole\Coroutine\MySQL->fetch() : ?array

Get the next item in result set.

<?php
$stmt = $db->prepare('SELECT * FROM ckl LIMIT 1');
$stmt->execute();
while($ret = $stmt->fetch())
{
    var_dump($ret);
}
Swoole\Coroutine\MySQL->fetchAll() : ?array

Get all items in the result set.

Example:

<?php
$stmt = $db->prepare('SELECT * FROM ckl LIMIT 1');
$stmt->execute();
$stmt->fetchAll();
Swoole\Coroutine\MySQL->nextResult() : ?bool

Get the next result in result set.

Example:

<?php
$stmt = $db->prepare('CALL reply(?)');
$res = $stmt->execute(['hello mysql!']);
do {
    var_dump($res);
} while ($res = $stmt->nextResult());
var_dump($stmt->affected_rows);

Example 2:

<?php
$stmt = $db->prepare('CALL reply(?)');
$stmt->execute(['hello mysql!']);
do {
    $res = $stmt->fetchAll();
    var_dump($res);
} while ($stmt->nextResult());
var_dump($stmt->affected_rows);