Skip to content

Commit 9cff1f8

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

File tree

1 file changed

+79
-23
lines changed

1 file changed

+79
-23
lines changed

docs/connect/php.md

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,96 @@
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+
use Amp\Postgres\PostgresConfig;
19+
use Amp\Postgres\PostgresConnectionPool;
20+
21+
$config = PostgresConfig::fromString("host=localhost user=crate");
22+
23+
$pool = new PostgresConnectionPool($config);
24+
25+
$statement = $pool->prepare("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3");
26+
27+
$result = $statement->execute();
28+
foreach ($result as $row) {
29+
print_r($row);
30+
}
31+
?>
32+
```
33+
:::{rubric} Example
34+
:::
35+
- [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)
36+
37+
## PostgreSQL PDO driver
38+
39+
[PDO_PGSQL] is a PHP-native driver that implements the PHP Data Objects (PDO)
40+
interface to enable access from PHP to PostgreSQL databases.
41+
42+
:::{rubric} Synopsis
43+
:::
44+
```php
45+
<?php
46+
$connection = new PDO("pgsql:host=localhost;port=5432;user=crate");
47+
$cursor = $connection->query("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3");
48+
print_r($cursor->fetchAll(PDO::FETCH_ASSOC));
49+
?>
50+
```
51+
52+
:::{rubric} Example
53+
:::
54+
- [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)
55+
56+
## CrateDB PDO driver
1057

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

14-
Example implementation will look like this:
15-
63+
:::{rubric} Synopsis
64+
:::
1665
```php
1766
<?php
1867

1968
require 'vendor/autoload.php';
2069

21-
use Crate\PDO\PDO as PDO;
22-
23-
$pdo = new PDO(
24-
'crate:<name-of-your-cluster>.cratedb.net:4200',
25-
'admin',
26-
'<PASSWORD>'
27-
);
70+
use Crate\PDO\PDOCrateDB;
2871

29-
$stm = $pdo->query('SELECT name FROM sys.cluster');
30-
$name = $stm->fetch();
31-
print $name[0];
72+
$dsn = '<DATA_SOURCE_NAME>';
73+
$user = 'crate';
74+
$password = null;
75+
$options = null;
76+
$connection = new PDOCrateDB($dsn, $user, $password, $options);
3277

78+
$stm = $connection->query("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3");
79+
$result = $stm->fetch();
80+
print_r($result);
3381
?>
3482
```
3583

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

38-
## DBAL
86+
## CrateDB DBAL adapter
3987

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

43-
Example implementation will look like this:
44-
94+
:::{rubric} Synopsis
95+
:::
4596
```php
4697
<?php
4798

@@ -56,12 +107,17 @@ $params = array(
56107
);
57108

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

113+
print_r($result);
64114
?>
65115
```
66116

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

0 commit comments

Comments
 (0)