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

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

Declaration

<?php OpenSwoole\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 OpenSwoole\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 OpenSwoole\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 OpenSwoole\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 September 1, 2022