Skip to content

v1 (stable)

/

Concepts

Connecting to SurrealDB

This guide shows how to initialise version 1 of the SurrealDB PHP SDK, connect to an instance and check on the connection. If you have not installed the SDK yet, see the installation guide.

MethodDescription
$db->connect($host, $options)Connects to a local or remote database endpoint
$db->close()Closes the persistent connection to the database
$db->use($options)Switches to a specific namespace and database
$db->health()Checks whether the database is running and the storage engine is healthy
$db->status()Checks whether the database is running and reachable
$db->version()Retrieves the version of the SurrealDB instance

Before we can make use of any of the packages we have installed for our project, we need to include the autoload.php file in our project. This file is generated by Composer and it contains all the classes and dependencies that we have installed in our project.

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

When we have imported the Surreal class, we can initialise a new instance of SurrealDB. After this is done we can make use of certain methods to interact with the database.

$db = new Surreal();

Then we connect to the database. The connect method takes the URL of the database as an argument. The URL can be either http or ws protocol.

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

In order to send queries to the database, we need to set the namespace and database for the current connection you want to interact with.

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

Connects to a local or remote database endpoint.

Method Syntax
$db->connect($host, $options)

Arguments

Type

Description

hoststring

The url of the database endpoint to connect to.

optionsassociative array

An object with options to initiate the connection to SurrealDB.

There are several ways to connect to a database endpoint. You can connect to a local or remote endpoint, specify a namespace and database pair to use, authenticate with an existing token, authenticate using a pair of credentials, or use advanced custom logic to prepare the connection to the database.

// Connect to a local endpoint
$db->connect('http://127.0.0.1:8000/rpc');

// Connect to a remote endpoint
$db->connect('https://cloud.surrealdb.com/rpc');

// Specify a namespace and database pair to use
$db->connect('https://cloud.surrealdb.com/rpc', [
	"namespace" => "surrealdb",
	"database" => "docs",
]);

Closes the persistent connection to the database.

Method Syntax
$db->close()
$db->close();

Switch to a specific namespace and database. If only the ns or db property is specified, the current connection details will be used to fill the other property.

Method Syntax
$db->use([ "namespace" => "...", "database" => "..." ]);

Arguments

Description

namespace

Switches to a specific namespace.

database

Switches to a specific database.

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

This method checks whether the database is running and the storage engine is running.

Note

This method is only available on a remote database targeted with the http protocol.

Method Syntax
$db->health();
$health = $db->health();
echo "The health status is: $health."; // 200 or 500

The health function returns 200 if the database is running and healthy, and 500 if a failure occurred.

This method retrieves the current status of a remote database.

Method Syntax
$db->status();
$status = $db->status();
echo "The status code is: $status."; // 200 or 500

The status function returns 200 if the database is running and 500 if the database is down or cannot be reached.

This method retrieves the current version of a remote database.

Method Syntax
$db->version();
try {
	$version = $db->version();
	echo "The remote database is running version $version.";
} catch (Exception $e) {
	echo "An error occurred while retrieving the version: " . $e->getMessage();
}

Was this page helpful?

Last updated