How to Connect with PHP
Connecting to CrateDB with PHP offers various drivers, such as DBAL and PDO, each presenting distinct functionalities to suit different development requirements. These drivers facilitate seamless integration between PHP applications and CrateDB, providing versatile options for developers to establish connections and interact with the database.
<?php
require 'vendor/autoload.php';
use Crate\PDO\PDO as PDO;
$pdo = new PDO(
'crate:teal-anakin-skywalker.aks1.eastus2.azure.cratedb.net:4200',
'admin',
''
);
$stm = $pdo->query('SELECT name FROM sys.cluster');
$name = $stm->fetch();
print $name[0];
?>
<?php
require 'vendor/autoload.php';
$params = array(
'driverClass' => 'Crate\DBAL\Driver\PDOCrate\Driver',
'user' => 'admin',
'password' = '',
'host' => 'teal-anakin-skywalker.aks1.eastus2.azure.cratedb.net',
'port' => 4200
);
$connection = \Doctrine\DBAL\DriverManager::getConnection($params);
$sql = 'SELECT name FROM sys.cluster';
$name = $connection->query($sql)->fetch();
print $name['name'];
?>
The DBAL driver for CrateDB, built on the PHP database abstraction layer, provides essential functionalities for schema management and interaction with CrateDB. It offers compatibility with Doctrine ORM, simplifying Object-Relational Mapping for PHP developers.
The PDO driver for CrateDB, following the PHP Data Objects standard, offers a flexible and standard interface for database access in PHP, including connection possibilities to CrateDB and CrateDB Cloud.