Swoole\Coroutine\PostgreSQL->query(...)

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Coroutine\PostgreSQL->query(string $sql): resource

Parameters

sql

The full SQL statement you want to execute with the client.

Return

When the query is successful it returns a PHP resource. If the query produced an error it will return false.


Description

Execute SQL statements in a non-blocking coroutine context. Enabling you to run SQL queries asynchronously.


Example

Selecting data

<?php

use Swoole\Coroutine\PostgreSQL;

Co\run(function()
{
    $pg = new PostgreSQL();

    $conn = $pg->connect("host=127.0.0.1;port=5432;dbname=test;user=postgres;password=***");

    $result = $pg->query('SELECT * FROM test;');
    $arr = $pg->fetchAll($result);
    var_dump($arr);
});

Returning insert ID

<?php

use Swoole\Coroutine\PostgreSQL;

Co\run(function()
{
    $pg = new PostgreSQL();

    $conn = $pg->connect("host=127.0.0.1;port=5432;dbname=test;user=postgres;password=***");

    $result = $pg->query("insert into test (id,text) VALUES (24,'text') RETURNING id;");
    $arr = $pg->fetchRow($result);
    var_dump($arr);
});

Transaction query

<?php

use Swoole\Coroutine\PostgreSQL;

Co\run(function()
{
    $pg = new PostgreSQL();

    $conn = $pg->connect("host=127.0.0.1;port=5432;dbname=test;user=postgres;password=***");

    $pg->query('BEGIN;');
    $result = $pg->query('SELECT * FROM test;');
    $arr = $pg->fetchAll($result);
    $pg->query('COMMIT;');

    var_dump($arr);
});
Last updated on August 31, 2022