This is a guide on how to initialize the SurrealDB SDK for PHP. If you haven’t installed the SDK yet in your PHP project, You can go to getting started to learn how to install the SDK.
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 initialize 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");
$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.
$db->use([ "namespace" => "test", "database" => "test" ]);
We should now be successfully connected to the database. In the next article we will cover authenticating our connection.