Skip to content

Commit 48e9f50

Browse files
committed
Driver/PHP: Add AMPHP and PDO_PGSQL
1 parent 9227a39 commit 48e9f50

File tree

1 file changed

+82
-23
lines changed

1 file changed

+82
-23
lines changed

docs/connect/php.md

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,99 @@
33
# PHP
44

55
:::{div} sd-text-muted
6-
Available PHP extensions for CrateDB and CrateDB Cloud.
6+
Available PHP drivers and adapters for CrateDB and CrateDB Cloud.
77
:::
88

9-
## PDO
9+
## AMPHP PostgreSQL driver
10+
11+
The [AMPHP PostgreSQL driver], `amphp/postgres`, is an asynchronous
12+
PostgreSQL client based on Amp.
13+
14+
:::{rubric} Synopsis
15+
:::
16+
```php
17+
<?php
18+
require 'vendor/autoload.php';
19+
20+
use Amp\Postgres\PostgresConfig;
21+
use Amp\Postgres\PostgresConnectionPool;
22+
use function Amp\async;
23+
use function Amp\Future\await;
24+
25+
await(async(function () {
26+
$config = PostgresConfig::fromString("host=localhost user=crate");
27+
$pool = new PostgresConnectionPool($config);
28+
$statement = $pool->prepare("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3");
29+
$result = $statement->execute();
30+
foreach ($result as $row) {
31+
print_r($row);
32+
}
33+
}));
34+
?>
35+
```
36+
:::{rubric} Example
37+
:::
38+
- [Connect to CrateDB and CrateDB Cloud using AMPHP/PostgreSQL] &nbsp; [![PHP AMPHP](https://github.com/crate/cratedb-examples/actions/workflows/lang-php-amphp.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-php-amphp.yml)
39+
40+
## PostgreSQL PDO driver
41+
42+
[PDO_PGSQL] is a PHP-native driver that implements the PHP Data Objects (PDO)
43+
interface to enable access from PHP to PostgreSQL databases.
44+
45+
:::{rubric} Synopsis
46+
:::
47+
```php
48+
<?php
49+
$connection = new PDO("pgsql:host=localhost;port=5432;user=crate");
50+
$cursor = $connection->query("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3");
51+
print_r($cursor->fetchAll(PDO::FETCH_ASSOC));
52+
?>
53+
```
54+
55+
:::{rubric} Example
56+
:::
57+
- [Use the PDO_PGSQL driver with CrateDB] &nbsp; [![PHP PDO](https://github.com/crate/cratedb-examples/actions/workflows/lang-php-pdo.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-php-pdo.yml)
58+
59+
## CrateDB PDO driver
1060

1161
The PHP Data Objects (PDO) is a standard PHP extension that defines a common
1262
interface for accessing databases in PHP.
63+
The {ref}`crate-pdo:index` implements this specification, wrapping access to
64+
CrateDB's HTTP interface.
1365

14-
Example implementation will look like this:
15-
66+
:::{rubric} Synopsis
67+
:::
1668
```php
1769
<?php
1870

1971
require 'vendor/autoload.php';
2072

21-
use Crate\PDO\PDO as PDO;
73+
use Crate\PDO\PDOCrateDB;
2274

23-
$pdo = new PDO(
24-
'crate:<name-of-your-cluster>.cratedb.net:4200',
25-
'admin',
26-
'<PASSWORD>'
27-
);
28-
29-
$stm = $pdo->query('SELECT name FROM sys.cluster');
30-
$name = $stm->fetch();
31-
print $name[0];
75+
$dsn = '<DATA_SOURCE_NAME>';
76+
$user = 'crate';
77+
$password = null;
78+
$options = null;
79+
$connection = new PDOCrateDB($dsn, $user, $password, $options);
3280

81+
$stm = $connection->query("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3");
82+
$result = $stm->fetch();
83+
print_r($result);
3384
?>
3485
```
3586

36-
See full documentation {ref}`here <crate-pdo:index>`.
87+
[![Tests](https://github.com/crate/crate-pdo/actions/workflows/tests.yml/badge.svg)](https://github.com/crate/crate-pdo/actions/workflows/tests.yml)
3788

38-
## DBAL
89+
## CrateDB DBAL adapter
3990

4091
DBAL is a PHP database abstraction layer that comes with database schema
4192
introspection, schema management, and PDO support.
93+
The {ref}`crate-dbal:index` implements this specification, wrapping access to
94+
CrateDB's HTTP interface. The adapter currently does not support recent
95+
versions of the Doctrine ORM.
4296

43-
Example implementation will look like this:
44-
97+
:::{rubric} Synopsis
98+
:::
4599
```php
46100
<?php
47101

@@ -56,12 +110,17 @@ $params = array(
56110
);
57111

58112
$connection = \Doctrine\DBAL\DriverManager::getConnection($params);
59-
$sql = 'SELECT name FROM sys.cluster';
60-
$name = $connection->query($sql)->fetch();
61-
62-
print $name['name'];
113+
$sql = "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3";
114+
$result = $connection->query($sql)->fetch();
63115

116+
print_r($result);
64117
?>
65118
```
66119

67-
See full documentation {ref}`here <crate-dbal:index>`.
120+
[![Tests](https://github.com/crate/crate-dbal/actions/workflows/tests.yml/badge.svg)](https://github.com/crate/crate-dbal/actions/workflows/tests.yml)
121+
122+
123+
[AMPHP PostgreSQL driver]: https://github.com/amphp/postgres
124+
[Connect to CrateDB and CrateDB Cloud using AMPHP/PostgreSQL]: https://github.com/crate/cratedb-examples/tree/main/by-language/php-amphp
125+
[PDO_PGSQL]: https://www.php.net/manual/en/ref.pdo-pgsql.php
126+
[Use the PDO_PGSQL driver with CrateDB]: https://github.com/crate/cratedb-examples/tree/main/by-language/php-pdo

0 commit comments

Comments
 (0)