LibSQL Class
query()
LibSQL PHP Extension method query()
The query()
method in the LibSQL PHP Extension facilitates the execution of an SQL query on the database. This method allows you to retrieve data from the database based on the provided SQL query. It supports parameterized queries for improved security and flexibility.
Method Signature
public function query(string $stmt, array $parameters = []): array
Parameters
$stmt
(string): The SQL query to execute.$parameters
(array): An optional array containing parameters for the SQL statement. The parameters can be provided as positional (numeric) or named parameters (assosiative).
Return Value
array
: Returns the result of the query as an associative array.
Example
// Create a new LibSQL instance
$db = new LibSQL("database.db");
// SQL query with parameterized query
$stmt = "SELECT * FROM users WHERE age > :age";
// Parameters for the parameterized query
$parameters = [':age' => 30];
// Execute the query
$result = $db->query($stmt, $parameters);
// Process the query result
foreach ($results['rows'] as $row) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Age: " . $row['age'] . "\n";
}
Notes
- Positional parameters are represented by
?
in the SQL statement and are replaced by values provided in the$parameters
array in the order they appear. - Named parameters are represented by placeholders like
:name
or@name
in the SQL statement and are replaced by corresponding key-value pairs in the$parameters
array. - The result of the query is returned as an associative array, where each element represents a row of the result set.
- Ensure proper error handling for database query failures to gracefully handle exceptions and errors during query execution.
Was this page helpful?