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.
| Method | Description |
|---|---|
$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 |
->query()
Runs a set of SurrealQL statements against the database.
$db->query($query, $vars)Arguments
Arguments | Type | Description |
|---|---|---|
$query | string | Specifies the SurrealQL statements. |
$vars | associative array | Assigns variables which can be used in the query. |
Example usage
// 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; ->queryRaw()
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.
$db->queryRaw($query, $params);Arguments
Arguments | Type | Description |
|---|---|---|
$query | string | The query to execute |
$params | associative array | An associative array of parameters to bind to the query. |
Example usage
// 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"'); ->run()
Runs a defined SurrealQL function on the remote database.
$db->run($function, $version, $params);Arguments
Arguments | Type | Description |
|---|---|---|
$function | string | The name of the function to run. |
$version |
| The version of the function to run. |
$params |
| An array of parameters to pass to the function. |
Example usage
$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"]); ->let()
Assigns a value as a parameter for this connection.
$db->let($name, $value)Arguments
Arguments | Type | Description |
|---|---|---|
name | string | Specifies the name of the variable. |
value | mixed | Assigns the value to the variable name. |
Example usage
// 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'); ->unset()
Removes a parameter for this connection.
$db->unset($key)Arguments
Arguments | Type | Description |
|---|---|---|
key | string | Specifies the name of the variable. |
Example usage
// Remove the variable from the connection
$db->unset('name');