pdo driver for CloudFlare D1 & php-wasm
pdo_cfd1 requires PHP 8.1
Simply pass the D1 object into the php-wasm constructor as cfd1
to enable pdo_cfd1 support. Once D1 is passed in, cfd1:
will be available as a PDO driver.
export async function onRequest(event)
{
const mainDb = event.env.mainDb;
const php = new PhpWorker({ cfd1: { mainDb } });
php.run(`<?php $pdo = new PDO('cfd1:mainDb');`);
}
You can check phpinfo()
to make sure that the D1 object is detected. CloudFlare D1 SQL module detected
will display "yes" when the object has been passed in correctly:
PDO can be used with D1 just like any other SQL server:
export async function onRequest(event)
{
const mainDb = event.env.mainDb;
const php = new PhpWorker({ cfd1: { mainDb } });
php.run(`<?php
$pdo = new PDO('cfd1:main');
$select = $pdo->prepare(
'SELECT PageTitle, PageContent FROM WikiPages WHERE PageTitle = ?'
);
$select->execute([$pageTitle]);
$page = $select->fetchObject();`
);
}
- Named replacement tokens - Currently only positional tokens are supported.
- Error handling - Error handling is currently very rudimentary and does not propagage messages.
pdo_cfd1
is powered by CloudFlare D1.