Use the Polypheny PHP Driver to run SQL and MongoQL from a PHP application. This guide installs the driver, writes and reads relational rows, and queries an existing document collection. The PHP driver reference covers connection options, transactions, value types, and metadata.
Prerequisites
- 64-bit PHP 8.2 or newer and Composer.
- A running Polypheny instance with a Prism 2.2 query interface.
- A Polypheny account with permission to create, write, read, and drop tables in its default relational namespace for the SQL example.
The examples use Prism TCP at 127.0.0.1:20590. Change the host, port, username, and password to match your instance. The example credentials are for a local setup with the default pa account and an empty password.
Install the driver
Run this command from your PHP project directory. Composer installs the package from Packagist and provides vendor/autoload.php for your application.
composer require polypheny/php-driver
Write and read rows with SQL
Save the following as sql-example.php in your project directory. The script creates a table with a unique name, inserts two rows using positional parameters, and commits the writes. The query binds a named parameter, then iterates through the rows with a fetch size of one. The driver fetches subsequent Prism frames as the loop advances.
<?php
use Polypheny\Connection;
require __DIR__ . '/vendor/autoload.php';
$connection = Connection::connectTcp('127.0.0.1', 20590, 'pa', '');
$table = 'php_integration_fruit_' . bin2hex(random_bytes(4));
try {
$cursor = $connection->cursor();
$cursor->execute("CREATE TABLE $table (
id INTEGER NOT NULL,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
)");
try {
$cursor->executeMany("INSERT INTO $table (id, name) VALUES (?, ?)", [
[1, 'Orange'],
[2, 'Pear'],
]);
$connection->commit();
$cursor->execute(
"SELECT id, name FROM $table WHERE id >= :minimum ORDER BY id",
['minimum' => 1],
fetchSize: 1,
);
foreach ($cursor as [$id, $name]) {
printf("%d: %s\n", $id, $name);
}
} finally {
$connection->rollback();
$cursor->execute("DROP TABLE $table");
}
} finally {
$connection->close();
}
php sql-example.php
The script prints 1: Orange and 2: Pear on separate lines, then drops the example table. Polypheny can commit DDL such as CREATE TABLE automatically, so the script explicitly drops the table during cleanup.
Read a document collection with MongoQL
Save this example as orders.php. It uses an existing document namespace named shop with an orders collection. Adjust the namespace, collection, and filter to match your data. Pass the namespace to executeAny(). Each document comes back as a stdClass.
<?php
use Polypheny\Connection;
require __DIR__ . '/vendor/autoload.php';
$connection = Connection::connectTcp('127.0.0.1', 20590, 'pa', '');
try {
$cursor = $connection->cursor();
$cursor->executeAny(
'mongo',
'db.orders.find({"status": "open"})',
fetchSize: 25,
namespace: 'shop',
);
foreach ($cursor as $order) {
echo json_encode($order, JSON_THROW_ON_ERROR), PHP_EOL;
}
} finally {
$connection->close();
}
php orders.php
The script prints one JSON object per matching document. To inspect a collection’s fields and indexes before querying, see the metadata examples in the PHP driver reference.