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

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);
});