1
0
mirror of https://github.com/dg/dibi.git synced 2025-01-18 06:38:45 +01:00
php-dibi/examples/connecting-to-databases.php

140 lines
2.7 KiB
PHP
Raw Normal View History

2017-07-21 21:34:37 +02:00
<?php
declare(strict_types=1);
?>
2010-08-03 12:28:07 +02:00
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
2018-05-10 21:45:11 +02:00
<h1>Connecting to Databases | Dibi</h1>
2010-08-03 12:28:07 +02:00
2008-07-17 03:51:29 +00:00
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
2008-07-17 03:51:29 +00:00
2018-05-10 21:45:11 +02:00
// connects to SQlite using Dibi class
2008-07-17 03:51:29 +00:00
echo '<p>Connecting to Sqlite: ';
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
'driver' => 'sqlite',
2014-05-13 15:56:44 +02:00
'database' => 'data/sample.s3db',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
echo 'OK';
} catch (Dibi\Exception $e) {
2008-07-17 03:51:29 +00:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to SQlite using Dibi\Connection object
2010-08-03 11:48:51 +02:00
echo '<p>Connecting to Sqlite: ';
try {
$connection = new Dibi\Connection([
'driver' => 'sqlite',
2014-05-13 15:56:44 +02:00
'database' => 'data/sample.s3db',
2015-10-06 01:39:01 +02:00
]);
2010-08-03 11:48:51 +02:00
echo 'OK';
} catch (Dibi\Exception $e) {
2010-08-03 11:48:51 +02:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to MySQLi
2010-08-03 11:48:51 +02:00
echo '<p>Connecting to MySQLi: ';
2008-07-17 03:51:29 +00:00
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-06-19 03:11:36 +02:00
'driver' => 'mysqli',
'host' => 'localhost',
2008-07-17 03:51:29 +00:00
'username' => 'root',
'password' => 'xxx',
'database' => 'dibi',
2015-10-06 01:39:01 +02:00
'options' => [
2015-06-19 03:11:36 +02:00
MYSQLI_OPT_CONNECT_TIMEOUT => 30,
2015-10-06 01:39:01 +02:00
],
2015-06-19 03:11:36 +02:00
'flags' => MYSQLI_CLIENT_COMPRESS,
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
echo 'OK';
} catch (Dibi\Exception $e) {
2008-07-17 03:51:29 +00:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to ODBC
echo '<p>Connecting to ODBC: ';
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-06-19 03:11:36 +02:00
'driver' => 'odbc',
2008-07-17 03:51:29 +00:00
'username' => 'root',
'password' => '***',
2018-04-17 10:03:43 +02:00
'dsn' => 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=' . __DIR__ . '/data/sample.mdb',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
echo 'OK';
} catch (Dibi\Exception $e) {
2008-07-17 03:51:29 +00:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to PostgreSql
echo '<p>Connecting to PostgreSql: ';
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-06-19 03:11:36 +02:00
'driver' => 'postgre',
'string' => 'host=localhost port=5432 dbname=mary',
'persistent' => true,
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
echo 'OK';
} catch (Dibi\Exception $e) {
2008-07-17 03:51:29 +00:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to PDO
echo '<p>Connecting to Sqlite via PDO: ';
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-06-19 03:11:36 +02:00
'driver' => 'pdo',
2014-06-02 16:50:01 +02:00
'dsn' => 'sqlite::memory:',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
echo 'OK';
} catch (Dibi\Exception $e) {
2008-07-17 03:51:29 +00:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
2015-10-22 01:53:41 +02:00
// connects to SQLSRV
2015-11-06 23:00:36 +01:00
echo '<p>Connecting to Microsoft SQL Server: ';
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-10-22 01:53:41 +02:00
'driver' => 'sqlsrv',
2015-06-19 03:11:36 +02:00
'host' => '(local)',
'username' => 'Administrator',
'password' => 'xxx',
'database' => 'main',
2015-10-06 01:39:01 +02:00
]);
echo 'OK';
} catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
2008-07-17 03:51:29 +00:00
// connects to Oracle
echo '<p>Connecting to Oracle: ';
try {
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-06-19 03:11:36 +02:00
'driver' => 'oracle',
2008-07-17 03:51:29 +00:00
'username' => 'root',
'password' => 'xxx',
'database' => 'db',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
echo 'OK';
} catch (Dibi\Exception $e) {
2008-07-17 03:51:29 +00:00
echo get_class($e), ': ', $e->getMessage(), "\n";
}
2011-07-01 08:06:36 +02:00
echo "</p>\n";