• Start

Languages

/

PHP

/

Language Starter

Data Querying

Learn how to interact with the database and perform CRUD operations using the SurrealDB SDK for PHP.

The methods below are used to interact with the database and perform CRUD operations. You can also use the query method to run SurrealQL statements against the database.

If we wish to create a new record in the database, we can use the create method. The first argument is the table name and the second argument is an associative array with the column names and values.

$person = $db->create("person:tobie", [
"name" => "Tobie",
"lastname" => "Morgan Hitchcock",
"age" => 30,
"hobbies" => ["reading", "coding"]
]);

After when you created a record, you can now use the select method to fetch the newly created person. The first argument is the newly created person's ID or a string which is the table name.

$person = $db->select($person->id);

Or you can fetch it manually by using the RecordID or RecordStringId.

// using the StringRecordId
$id = StringRecordId::create("person:tobie");
$person = $db->select($id);

// using the RecordId
$id = RecordId::create("person", "tobie");
$person = $db->select($id);

To update a record, you can use the update method. The first argument is the RecordID or a StringRecordId, and the second argument is an associative array with the column names and values. Updating a record can be done if 3 ways:

The update method will replace the entire record with the new values. So make sure you include all the columns in the associative array.

		$person = $db->update($person->id, [
"name" => "Tobie",
"lastname" => "Morgan Hitchcock",
"age" => 31
]);

You can find more information about updating a record using the update method here.

To delete a record, you can use the delete method. The first argument is the RecordID or a StringRecordId.

$db->delete($person->id);

or we can use the RecordId or StringRecordId to delete the record.

$id = StringRecordId::create("person:tobie");
$db->delete($id);

$id = RecordId::create("person", "tobie");
$db->delete($id);

Was this page helpful?