DownloadCloudflare D1 PHP Client
A lightweight, framework-agnostic PHP client for interacting with Cloudflare D1 databases.
Requirements
-
PHP 7.4 or higher
-
cURL extension
-
JSON extension
-
A Cloudflare account with D1 database access
Installation
composer require yerikmiller/cloudflare-d1-php
Usage
Basic Setup
require 'vendor/autoload.php';
use Cloudflare\D1\D1;
// Initialize the D1 client
D1::$accountId = 'your-account-id';
D1::$apiToken = 'your-api-token';
D1::$databaseId = 'your-database-id';
// Create a new D1 instance whatever you want
$d1 = new D1();
Executing Queries
Fetching Data
// Get all rows from a table
$results = $d1->get('SELECT * FROM users');
// Get a single row
$user = $d1->first('SELECT * FROM users WHERE id = ?', [1]);
// Get a single value
$email = $d1->value('SELECT email FROM users WHERE id = ?', [1]);
Modifying Data
// Insert a new record
$affectedRows = $d1->execute(
'INSERT INTO users (name, email) VALUES (?, ?)',
['John Doe', '[email protected]']
);
// Update a record
$affectedRows = $d1->execute(
'UPDATE users SET name = ? WHERE id = ?',
['John Updated', 1]
);
// Delete a record
$affectedRows = $d1->execute('DELETE FROM users WHERE id = ?', [1]);
Working with Results
// Get all users
$users = $d1->get('SELECT * FROM users');
foreach ($users as $user) {
echo "User: {$user['name']} ({$user['email']})\n";
}
// Get the last insert ID
$id = $d1->execute(
'INSERT INTO users (name, email) VALUES (?, ?)',
['New User', '[email protected]']
);
$lastInsertId = $d1->value('SELECT last_insert_rowid()');
Error Handling
try {
$result = $d1->query('SELECT * FROM non_existent_table');
} catch (\Cloudflare\D1\D1Exception $e) {
echo "Error: " . $e->getMessage();
}
Testing
composer test
License
This project is licensed under the MIT License - see the LICENSE file for details.
|