PHP Classes

How to Use a Cloudflare D1 PHP Client Class to Execute SQL Queries to Databases in the Cloudflare D1 Web Service Using the Package Cloudflare D1 PHP: Query databases stored in Cloudflare D1 using SQL

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-09-30 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
cloudflare-d1-php 1.0Custom (specified...7Databases, Web services, PHP 7
Description 

Author

This package can query databases stored in Cloudflare D1 using SQL.

It can send HTTP requests to Cloudflare D1 API Web server to request the execution of given SQL queries.

The class can:

- Execute a SQL query and return a result object

- Execute a SQL query and return the first result row

- Execute a SQL query and return a single row

- Execute a SQL query and return the number of affected rows

Innovation Award
PHP Programming Innovation award nominee
September 2025
Number 6
Cloudflare is company that provides several types of services useful to site owners.

Cloudflare D1 is a service that provides access to a remote SQL database that can be accessed via a Web services API.

This package provides a client class that can access the Cloudflare D1 service.

Manuel Lemos
Picture of Yorman
  Performance   Level  
Name: Yorman <contact>
Classes: 2 packages by
Country: Venezuela Venezuela
Innovation award
Innovation award
Nominee: 1x

Instructions

Please read this document to learn how execute SQL queries in databases stored in Cloudflare D1.

Documentation

Cloudflare 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', 'john@example.com']
);

// 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', 'new@example.com']
);
$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.


  Files folder image Files (6)  
File Role Description
Files folder imagesrc (3 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  src  
File Role Description
  Plain text file D1.php Class Class source
  Plain text file D1Exception.php Class Class source
  Plain text file D1Result.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0