LibSQL Class
On this page, you will find more information about the libSQL PHP classes and available functions.
The LibSQL
class provides an interface for connecting to and interacting with a LibSQL database. It supports operations such as preparing and executing SQL statements, managing transactions, and retrieving query results.
Constants
OPEN_READONLY
: Specifies read-only mode when opening the database connection.OPEN_READWRITE
: Specifies read-write mode when opening the database connection.OPEN_CREATE
: Specifies create mode when opening the database connection.LIBSQL_ASSOC
: Return results as an associative array.LIBSQL_NUM
: Return results as a numerical array.LIBSQL_BOTH
: Return results as both associative and numerical arrays.LIBSQL_ALL
: Return all result sets.LIBSQL_LAZY
: Return a result generator.
Properties
$mode
: (string)
The mode of the database connection.
Methods
__construct
Creates a new instance of the LibSQL
class to establish a connection to the database.
Parameters
string|array $config
: Configuration for the database connection.- Examples: Local file path, DSN string, or an array of configuration parameters.
int|null $flags
(optional): Connection flags (default:6
- read-write and create mode).string|null $encryption_key
(optional): The encryption key for the database.
Example
version
(static)
Retrieves the version of the LibSQL library and PHP extension.
Returns
- (string): Version information.
Example
changes
Gets the number of rows changed by the last executed SQL statement.
Returns
- (int): The number of rows changed.
Example
isAutocommit
Checks if autocommit mode is enabled for the connection.
Returns
- (bool):
true
if autocommit is enabled, otherwisefalse
.
Example
totalChanges
Gets the total number of rows changed since the database connection was opened.
Returns
- (int): Total number of rows changed.
Example
lastInsertedId
Retrieves the ID of the last inserted row.
Returns
- (int): The ID of the last inserted row.
Example
execute
Executes a single SQL statement on the database.
Parameters
string $stmt
: The SQL statement to execute.array $parameters
(optional): Parameters for the SQL statement.
Returns
- (int): Number of rows affected by the statement.
Example
executeBatch
Executes a batch of SQL statements.
Parameters
string $stmt
: The batch of SQL statements to execute.
Returns
- (bool):
true
if the batch execution was successful, otherwisefalse
.
Example
query
Executes an SQL query and retrieves the result set.
Parameters
string $stmt
: The SQL query to execute.array $parameters
(optional): Parameters for the query.
Returns
- (LibSQLResult): The result of the query.
Example
Read more detail about LibSQLResult Class
transaction
Starts a new database transaction.
Parameters
string $behavior
(optional): Transaction behavior (default:"DEFERRED"
).
Returns
- (LibSQLTransaction): The transaction object.
Example
Read more detail about LibSQLTransaction Class
prepare
Prepares an SQL statement for execution.
Parameters
string $sql
: The SQL statement to prepare.
Returns
- (LibSQLStatement): The prepared statement object.
Example
Read more detail about LibSQLStatement Class
close
Closes the database connection.
Returns
- (void)
Example
sync
Syncs the database to ensure data integrity.
Returns
- (void)
Example
enableLoadExtension
Enables or disables the loading of extensions.
Parameters
bool $onoff
:true
to enable extensions,false
to disable.
Returns
- (void)
Example
loadExtensions
Loads specified database extensions.
Parameters
array|string $extension_paths
: Paths to the extensions to load.
Returns
- (void)