-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinsert_basic.php
43 lines (34 loc) · 1.09 KB
/
insert_basic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/*
* Basic example demonstrating how to connect to CrateDB using PHP PDO.
*
* Prerequisites:
*
* docker run --rm -it --publish=4200:4200 crate
*
* Synopsis:
*
* php examples/insert_basic.php
*/
declare(strict_types=1);
include("./vendor/autoload.php");
error_reporting(E_ALL);
// Connect to CrateDB.
use Crate\PDO\PDOCrateDB;
$connection = new PDOCrateDB("crate:localhost:4200", "crate");
// Create database table.
$connection->exec("DROP TABLE IF EXISTS test_table;");
$connection->exec("CREATE TABLE test_table (id INTEGER, name STRING, int_type INTEGER);");
// Run insert operation.
$statement = $connection->prepare('INSERT INTO test_table (id, name, int_type) VALUES (?, ?, ?)');
$statement->execute([5, 'foo', 1]);
$statement->execute([6, 'bar', 2]);
// Evaluate response.
print("Total count: {$statement->rowCount()}\n");
$response = $statement->fetchAll(PDO::FETCH_NUM);
print_r($response);
// Disconnect from database.
// https://www.php.net/manual/en/pdo.connections.php
// https://stackoverflow.com/questions/18277233/pdo-closing-connection
$statement = null;
$connection = null;