承接 yerikmiller/cloudflare-d1-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yerikmiller/cloudflare-d1-php

Composer 安装命令:

composer require yerikmiller/cloudflare-d1-php

包简介

A lightweight PHP client for Cloudflare D1 database

README 文档

README

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.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-25