Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\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 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);
});