Skip to content

v1 (stable)

/

Concepts

Executing queries

The methods below run SurrealQL statements against the database, call a SurrealQL function, and keep variables on the connection that later queries can use. To read and write records with a method for each operation, see data manipulation.

MethodDescription
$db->query($query, $vars)Runs a set of SurrealQL statements against the database
$db->queryRaw($query, $params)Runs SurrealQL statements and returns the raw, unprocessed response
$db->run($function, $version, $params)Runs a SurrealQL function on the server
$db->let($name, $value)Assigns a value as a parameter for this connection
$db->unset($key)Removes a parameter for this connection

Runs a set of SurrealQL statements against the database.

Method Syntax
$db->query($query, $vars)

Arguments

Type

Description

$querystring

Specifies the SurrealQL statements.

$varsassociative array

Assigns variables which can be used in the query.

// Assign the variable on the connection
$result = $db->query(
	'CREATE person SET name = "John"; SELECT * FROM type::table($tb);',
	[ "tb" => "person" ]
);

// Get the first result from the first query
$created = $result[0]->result[0];

// Get all of the results from the second query
$people = $result[1]->result;

With queryRaw(), you get back the raw RPC response. In contrast to the query() method, this does not throw for errors that occur in individual queries, but gives those back as a string, and this includes the time it took to execute the individual queries.

Method Syntax
$db->queryRaw($query, $params);

Arguments

Type

Description

$querystring

The query to execute

$paramsassociative array

An associative array of parameters to bind to the query.

Example
// Query users with the firstname "Tobie", using parameters.
$response = $db->queryRaw(
	'SELECT * FROM users WHERE firstname = $firstname',
	['firstname' => 'Tobie']
);

// The same query without parameters.
$response = $db->queryRaw('SELECT * FROM users WHERE firstname = "Tobie"');

Runs a defined SurrealQL function on the remote database.

Method Syntax
$db->run($function, $version, $params);

Arguments

Type

Description

$functionstring

The name of the function to run.

$version

string or null

The version of the function to run.

$params

array or null

An array of parameters to pass to the function.

Example
$functionA = $db->run("fn::hello_world", null, ["Tobie"]);
$functionB = $db->run("fn::hello_world", "v1", ["Tobie"]);

// or with named arguments
$functionA = $db->run("fn::hello_world", params: ["Tobie"]);
$functionB = $db->run("fn::hello_world", version: "v1", params: ["Tobie"]);

Assigns a value as a parameter for this connection.

Method Syntax
$db->let($name, $value)

Arguments

Type

Description

namestring

Specifies the name of the variable.

valuemixed

Assigns the value to the variable name.

// Assign the variable on the connection
$db->let('name', [
	"first" => "Tobie",
	"last" => "Morgan Hitchcock",
]);

// Use the variable in a subsequent query
$db->query('CREATE person SET name = $name');

// Use the variable in a subsequent query
$db->query('SELECT * FROM person WHERE name.first = $name.first');

Removes a parameter for this connection.

Method Syntax
$db->unset($key)

Arguments

Type

Description

keystring

Specifies the name of the variable.

// Remove the variable from the connection
$db->unset('name');

Was this page helpful?

Last updated