OpenSwoole Async MySQL client

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

This class or function have been deprecated.

The swoole contains the swoole async MySQL client. It has realized the protocal of MySQL and is a replacement of the other sync MySQL clients: libmysqlclient, mysqlnd, mysqli.

Example:
<?php
$db = new swoole_mysql;
$server = array(
    'host' => '192.168.56.102',
    'port' => 3306,
    'user' => 'test',
    'password' => 'test',
    'database' => 'test',
    'charset' => 'utf8',
    'timeout' => 2,
);

$db->connect($server, function ($db, $r) {
    if ($r === false) {
        var_dump($db->connect_errno, $db->connect_error);
        die;
    }
    $sql = 'show tables';
    $db->query($sql, function(swoole_mysql $db, $r) {
        if ($r === false)
        {
            var_dump($db->error, $db->errno);
        }
        elseif ($r === true )
        {
            var_dump($db->affected_rows, $db->insert_id);
        }
        var_dump($r);
        $db->close();
    });
});
Events
  • connect
OpenSwoole\MySQL::construct()

Alias: swoole_mysql->construct()

OpenSwoole\MySQL::on($event_name, callable $callback)

Alias: swoole_mysql->on($event_name, callable $callback)

Register callback function based on event name, current only 'Clost' event is supported.

OpenSwoole\MySQL::connect(array $serverConfig, callable $callback)

Alias: swoole_mysql->connect(array $serverConfig, callable $callback);

Connect to the remote MySQL server.

Example:

<?php
// serverConfig
$server = array(
    'host' => '192.168.56.102',
    'user' => 'test',
    'password' => 'test',
    'database' => 'test',
    'charset' => 'utf8',
);

// callback function
function onConnect(swoole_mysql $db, bool $result);
OpenSwoole\MySQL::escape(string $str)

Alias: swoole_mysql->escape(string $str)

Escape SQL strings to avoid SQL injection attacks.

Example:

<?php
$db = new swoole_mysql;
$server = array(
    'host' => '127.0.0.1',
    'user' => 'root',
    'password' => 'root',
    'database' => 'test',
);
$db->connect($server, function ($db, $result) {
    $data = $db->escape("abc'efg\r\n");
});
OpenSwoole\MySQL::query($sql, callable $callback)

Alias: swoole_mysql->query($sql, callable $callback)

Run SQL query.

Callback function:

function onSQLReady(swoole_mysqli $link, mixed $result);

OpenSwoole\MySQL::begin(callable $callback)

Alias: swoole_mysql->begin(callable $callback)

Start a MySQL transaction.

Example:

<?php
$db->begin(function( $db, $result) {
    $db->query("update userinfo set level = 22 where id = 1", function($db, $result) {
        $db->rollback(function($db, $result) {
            echo "commit ok\n";
        });
    });
});
OpenSwoole\MySQL::commit(callable $callback)

Alias: swoole_mysql->commit(callable $callback)

Commit the MySQL transaction.

Example:

<?php
$db->begin(function( $db, $result) {
    $db->query("update userinfo set level = 22 where id = 1", function($db, $result) {
        $db->commit(function($db, $result){
            echo "commit ok\n";
        });
    });
});
OpenSwoole\MySQL::rollback(callable $callback)

Alias: swoole_mysql->rollback(callable $callback)

Rollback the MySQL transaction.

OpenSwoole\MySQL->close()

Alias: swoole_mysql->close()

Close the MySQL connection.

Last updated on September 1, 2022