# PHP

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

The PHP SDK for SurrealDB lets you connect to a database and query it from your application. This guide covers connecting, authenticating, and running your first queries.

> [!NOTE]
> This guide covers the stable **v1** line of the PHP SDK. A **v2** rewrite is available in alpha. See the [v2 (alpha) getting-started guide](/docs/reference/php/versions/v2-alpha.md).

## 1. Install the SDK

Follow the [installation guide](/docs/reference/php/v1/installation.md) to add the SDK to your project. Once installed, include the autoloader and import the `Surreal` class.

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

use Surreal\Surreal;

$db = new Surreal();
```

## 2. Connect to SurrealDB

Use `connect()` to open a connection, then `use()` to select the namespace and database you want to work with, and `signin()` to authenticate. Most operations require both.

Supported connection protocols include:
- **WebSocket** (`ws://`, `wss://`) for long-lived stateful connections
- **HTTP** (`http://`, `https://`) for short-lived stateless connections

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

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

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

## 3. Inserting data into SurrealDB

Once connected, use `create()` to create records. Pass a record ID such as `person:tobie` to specify the identifier explicitly, or a table name to generate a random ID.

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

## 4. Retrieving data from SurrealDB

### Selecting records

The `select()` method retrieves all records from a table, or a single record by its ID.

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

### Running SurrealQL queries

For more advanced use cases, use `query()` to run [SurrealQL](/docs/reference/query-language.md) statements directly. Use [parameters](/docs/reference/query-language/language-primitives/parameters.md) to safely pass dynamic values.

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

## 5. Closing the connection

Always close the connection when you are done to release resources.

```php
$db->close();
```

## Next steps

You have learned how to install the SDK, connect to SurrealDB, create records, and retrieve data. There is a lot more you can do with the SDK, including updating and deleting records, authentication, live queries, and transactions.

- [Connection management](/docs/reference/php/v1/concepts/connecting.md) - Learn how to manage your database connections, including protocols and configuration.

- [Authentication](/docs/reference/php/v1/concepts/authentication.md) - Read more about authentication and how to integrate it into your application.

- [Executing queries](/docs/reference/php/v1/concepts/executing-queries.md) - Create, select, update, and delete records in depth.

- [API Reference](/docs/reference/php/v1/methods.md) - The full method reference for the Surreal class.

> [!NOTE]
> This getting-started guide covers the essentials. For the complete methods, API, and concept reference, see the [PHP (v1) SDK reference](/docs/reference/php/v1.md).
