Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Coroutine\PostgreSQL->query(string $sql): resource
The full SQL statement you want to execute with the client.
When the query is successful it returns a PHP resource
. If the query produced an error it will return false
.
Execute SQL statements in a non-blocking coroutine context. Enabling you to run SQL queries asynchronously.
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);
});