In-Memory Connection
An in-memory connection creates a temporary database stored entirely in memory. This database disappears when the script ends.
Example
<?php
// Connect to an in-memory database
$libsql = new LibSQL("file::memory:");
// Create schema
$libsql->execute("CREATE TABLE sessions (id INTEGER PRIMARY KEY, token TEXT)");
// Insert data
$libsql->execute("INSERT INTO sessions (token) VALUES (?)", ["abc123"]);
// Query data
$result = $libsql->query("SELECT * FROM users");
$rows = $result->fetchArray(LibSQL::LIBSQL_ASSOC);
foreach ($rows as $row) {
echo $row["id"] . " - " . $row["name"] . PHP_EOL;
}
When to Use
- Unit testing
- Temporary datasets
- Caching scenarios where persistence isnβt needed
Next Steps
- π Remote Connection β Connect libSQL remotely
- π Embedded Replica Connection β Connect libSQL with Embedded Replica
- π Offline Writes (Turso) Connection β Connect libSQL with Offline Writes abillity from (with Turso)
- π Offline Writes (libSQL Server/sqld) Connection β Connect libSQL with Offline Writes abillity from (with libSQL Server - self-host)