PHP (PDO)
PHP Data Objects (PDO) is a PHP extension that provides a consistent interface for accessing databases. Because Redpanda SQL implements the PostgreSQL wire protocol, you can use PDO’s PostgreSQL driver to connect from PHP. This page describes how to use PHP PDO with Redpanda SQL and lists unsupported functions and structures.
Establish a SQL connection
conn = new PDO(
"pgsql:host=<host>;port=<port>;dbname=<database>",
"<username>",
"<password>",
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => true,
]);
Note that the PDO::ATTR_EMULATE_PREPARES attribute is set to true, which is required in Redpanda SQL to ensure stability of query execution. Without this attribute setup, you may encounter prepared statement errors during queries execution:
ERROR: prepared statement [...]
|
If you are running Redpanda Cloud, you can append |
Example usage
This example shows basic query execution, once the connection has been established:
$stmt = $conn->prepare("SELECT :number as num;", [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$stmt->execute(['number' => 1234]);
$res = $stmt->fetchAll();
print_r($res)
Unsupported functions and structures
The following functions and potentially related structures are not currently supported when working with Redpanda SQL and PHP PDO:
-
PDO::pgsqlLOBCreate,pgsqlLOBOpen- Large Objects -
PDO::pgsqlGetPid- returning processes ID -
PDO::pgsqlCopytFromFile,PDO::pgsqlCopytFromArray- copy from STDIN -
PDO::pgsqlCopytToFile- copy to STDIN -
PDO::pgsqlCopytToArray- copy to STDOUT -
PDO::lastInsertId- SEQUENCES -
PDO::beginTransaction,PDO::inTransaction,PDO::commit,PDO::rollBack- Transactions -
PDOStatement::rowCount- may return incorrect row counts for some operations