• Start

Languages

/

PHP

/

v1 (stable)

Quickstart

Connect to SurrealDB with version 1 of the PHP SDK and run your first queries.

This guide connects to a SurrealDB instance with version 1 of the PHP SDK and runs a few basic operations.

Follow the installation guide to add the SDK to your project, then include the autoloader and import the Surreal class.

require __DIR__ . '/vendor/autoload.php';

use Surreal\Surreal;

$db = new Surreal();

Connect to the instance, then select a namespace and database with use().

$db->connect("ws://127.0.0.1:8000/rpc");

$db->use([
"namespace" => "surrealdb",
"database" => "docs",
]);

Sign in with your credentials. See Authentication for namespace, database, and record access.

$token = $db->signin([
"username" => "root",
"password" => "root",
]);

Use create() to add a record and select() to read it back.

$person = $db->create("person:tobie", [
"name" => "Tobie",
"age" => 32,
]);

$everyone = $db->select("person");

Use query() for raw SurrealQL, passing variables as the second argument.

$result = $db->query(
'SELECT * FROM person WHERE age > $min',
["min" => 18]
);
$db->close();

Was this page helpful?