Skip to content

seanmorris/pdo-cfd1

Repository files navigation

pdo-cfd1

pdo driver for CloudFlare D1 & php-wasm

Static Badge Discord

Join the community: reddit | discord | php-wasm

Coming Soon! Tutorial on getting php-wasm running in CloudFlare.

pdo_cfd1 requires PHP 8.1

Connect & Configure

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();`
    );
}

Todo

  • Named replacement tokens - Currently only positional tokens are supported.
  • Error handling - Error handling is currently very rudimentary and does not propagage messages.

CloudFlare D1

pdo_cfd1 is powered by CloudFlare D1.

https://developers.cloudflare.com/d1/

About

pdo driver for CloudFlare D1 & php-wasm

Resources

Stars

Watchers

Forks