-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_connection.php
53 lines (43 loc) · 1.21 KB
/
test_connection.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
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Test script to connect to Databricks SQL endpoint from PHP.
*/
require(__DIR__ . '/config.php');
$dsn = 'Databricks';
$user = 'token';
// NOTE $password is defined in config.php.
// Try to connect via ODBC.
echo "Trying ODBC connection...\n";
$odbc = odbc_connect($dsn, $user, $password);
// Checking connection id or reference
if (!$odbc) {
echo "ODBC connect failed: " . odbc_error() . "\n";
} else {
echo "ODBC connection successful!\n";
$sql = 'SHOW DATABASES';
echo 'Query: ' . $sql . "\n";
$results = odbc_exec($odbc, $sql);
while (odbc_fetch_row($results)) {
for ($i=1; $i<=odbc_num_fields($results); $i++) {
echo odbc_result($results, $i) . "\n";
}
}
// Resource releasing
odbc_close($odbc);
}
// Next try PDO.
try {
echo "\nTrying PDO connection...\n";
$dsn = 'odbc:Databricks';
$pdo = new PDO($dsn, $user, $password);
echo "PDO connection successful!\n";
$sql = 'SHOW DATABASES';
echo 'Query: ' . $sql . "\n";
$results = $pdo->query($sql);
foreach ($results as $row) {
echo $row[0] . "\n";
}
} catch (Exception $e) {
echo "PDO connection failed: " . $e->getMessage() . "\n";
}
echo "\nDONE!\n";