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

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

Declaration

<?php OpenSwoole\Coroutine\PostgreSQL->escape(string $string): string|false

Parameters

string

The string to escape for use inside a quoted SQL string literal.

Return

Returns the escaped string without the surrounding quotes. Returns false on failure or when the result is empty (for example for an empty input string), check the error and errCode properties.


Description

Escape a string for use inside a single quoted SQL string literal, using the encoding of the current connection. The returned value does not include the quotes, add them yourself.

The client must be connected. Prefer prepare() with parameters when you can.


Example

<?php

use OpenSwoole\Coroutine\PostgreSQL;

co::run(function()
{
    $pg = new PostgreSQL();
    $pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");

    $name = $pg->escape("O'Reilly");
    var_dump($name); // string(9) "O''Reilly"

    $stmt = $pg->query("SELECT * FROM users WHERE name = '{$name}'");
    var_dump($stmt->fetchAll());
});
Last updated on November 20, 2026