The built-in database
Use the zero-config SQLite drop-in on your preview server.
Every preview server includes a zero-config SQLite database. There are no credentials to manage and nothing to provision: require the drop-in and start querying.
Connecting
A helper file, zizhost-db.php, is available on the preview server. It returns a ready PDO handle:
<?php
require_once 'zizhost-db.php';
$db = zizhost_db();
$db->exec("CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY,
body TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)");
$stmt = $db->prepare("INSERT INTO notes (body) VALUES (?)");
$stmt->execute(['hello world']);
foreach ($db->query("SELECT * FROM notes ORDER BY id DESC") as $row) {
echo htmlspecialchars($row['body']), "<br>";
}
The handle is a standard PDO object in exception mode, so use prepared statements for anything user-supplied, as you would with MySQL.
Where the data lives
The database file lives on the preview server, which is temporary, so the data resets when the server is recycled. That suits prototyping and demos. For durable storage on a live site, use a MySQL database on your hosting account.
Tip
The toolbar shows a Database button once a preview server is running. It shows this same snippet for copy-paste.