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.
| Method | Description |
|---|---|
$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 |
Initialise and connect
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"
]); ->connect()
Connects to a local or remote database endpoint.
$db->connect($host, $options)Arguments
Arguments | Type | Description |
|---|---|---|
host | string | The url of the database endpoint to connect to. |
options | associative array | An object with options to initiate the connection to SurrealDB. |
Example usage
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",
]); ->close()
Closes the persistent connection to the database.
$db->close()Example usage
$db->close(); ->use()
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.
$db->use([ "namespace" => "...", "database" => "..." ]);Arguments
Arguments | Description |
|---|---|
namespace | Switches to a specific namespace. |
database | Switches to a specific database. |
Example usage
$db->use([
"namespace" => "surrealdb",
"database" => "docs"
]); ->health()
This method checks whether the database is running and the storage engine is running.
This method is only available on a remote database targeted with the http protocol.
$db->health();Example usage
$health = $db->health();
echo "The health status is: $health."; // 200 or 500The health function returns 200 if the database is running and healthy, and 500 if a failure occurred.
->status()
This method retrieves the current status of a remote database.
$db->status();Example usage
$status = $db->status();
echo "The status code is: $status."; // 200 or 500The status function returns 200 if the database is running and 500 if the database is down or cannot be reached.
->version()
This method retrieves the current version of a remote database.
$db->version();Example usage
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();
}