---
title: "Connecting to SurrealDB"
description: "Initialise version 1 of the PHP SDK, connect to an instance over HTTP or WebSocket, select a namespace and database, and check the server with health, status and version."
canonical_url: "https://surrealdb.com/docs/reference/php/v1/concepts/connecting-to-surrealdb"
sdk: "PHP SDK v1"
surrealdb_version: "4.x"
last_modified: "2026-11-24T05:29:47Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# Connecting to SurrealDB

Initialise version 1 of the PHP SDK, connect to an instance over HTTP or WebSocket, select a namespace and database, and check the server with health, status and version.

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](/docs/reference/php/v1/installation.md).

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

## Initialise and connect

**Composer**

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.
		```php
		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.

```php
$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.

**HTTP**

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

**WebSocket**

		```php
		$db->connect("ws://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.

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

## `->connect()` {#connect}

Connects to a local or remote database endpoint.

```php title="Method Syntax"
$db->connect($host, $options)
```

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
			<th colspan="2" scope="col">Type</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>host</code>
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>string</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                The url of the database endpoint to connect to.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>options</code>
            </td>
			<td colspan="2" scope="row" data-label="Type">
				<code>associative array</code>
			</td>
            <td colspan="2" scope="row" data-label="Description">
                An object with options to initiate the connection to SurrealDB.
            </td>
        </tr>
    </tbody>
</table>

### 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.

```php
// 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()` {#close}

Closes the persistent connection to the database.

```php title="Method Syntax"
$db->close()
```

### Example usage
```php
$db->close();
```

## `->use()` {#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.

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

### Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Arguments</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>namespace</code>
                <label label="initially required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Switches to a specific namespace.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Arguments">
                <code>database</code>
                <label label="initially required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Switches to a specific database.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```php
$db->use([
    "namespace" => "surrealdb",
    "database" => "docs"
]);
```

## `->health()` {#health}

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.

```php title="Method Syntax"
$db->health();
```

### Example usage
```php
$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.

## `->status()` {#status}

This method retrieves the current status of a remote database.

```php title="Method Syntax"
$db->status();
```

### Example usage
```php
$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.

## `->version()` {#version}

This method retrieves the current version of a remote database.

```php title="Method Syntax"
$db->version();
```

### Example usage
```php
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();
}

```
