Swoole Coroutine Postgres

Swoole\Coroutine\Postgres

Coroutine PHP PostgreSQL Client.

You can use Postgres client within coroutine context.

Pre-requirement

  • libpq is required.
  • ext-postgresql is requried.

Install ext-postgresql

git clone [email protected]:swoole/ext-postgresql.git
phpize
./configure
make && make install

Then add extension=swoole_postgresql.so to your php.ini file to enable the Swoole Postgres extension.

How to use Swoole Postgres Client

The methods available are same as the original PHP PostgreSQL client.

Example:

<?php
Co\run(function () {
    $db = new Swoole\Coroutine\PostgreSQL();
    $db->connect("host=127.0.0.1 port=5432 dbname=test user=root password=password");
    $db->prepare('fortunes', 'SELECT id, message FROM Fortune');
    $res = $db->execute('fortunes', []);
    $arr = $db->fetchAll($res);
    var_dump($arr);

    $db->prepare('select_query', 'SELECT id, randomnumber FROM World WHERE id = $1');
    $res = $db->execute('select_query', [123]);
    $ret = $db->fetchAll($res);
    var_dump($ret);
});