-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_pdo.php
65 lines (63 loc) · 2.24 KB
/
connection_pdo.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
54
55
56
57
58
59
60
61
62
63
64
65
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function getPDOConnection() {
$config = include('config/config_pdo.php');
$driver = $config['driver'];
$server = $config['server'];
$dbname = $config['dbname'];
$username = $config['username'];
$password = $config['password'];
switch ($driver) {
case 'mysql':
case 'mariadb':
$dsn = "mysql:host=$server;dbname=$dbname";
break;
case 'pgsql':
$dsn = "pgsql:host=$server;dbname=$dbname";
break;
case 'sqlite':
$dsn = "sqlite:$dbname";
break;
case 'sqlsrv':
$dsn = "sqlsrv:Server=$server;Database=$dbname";
break;
case 'oci': // Oracle
$dsn = "oci:dbname=//$server/$dbname";
break;
case 'ibm': // IBM DB2
$dsn = "ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$dbname;HOSTNAME=$server;PORT=50000;PROTOCOL=TCPIP;";
break;
case 'odbc': // ODBC
$dsn = "odbc:DSN=$dbname";
break;
case 'firebird': // Firebird
$dsn = "firebird:dbname=$server:$dbname";
break;
case 'cubrid': // CUBRID
$dsn = "cubrid:host=$server;port=33000;dbname=$dbname";
break;
case 'informix': // Informix
$dsn = "informix:host=$server;service=1526;database=$dbname;server=ids_server;protocol=onsoctcp;";
break;
case 'access': // Microsoft Access
$dsn = "odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbname;";
break;
case 'sybase': // Sybase
$dsn = "odbc:Driver={Adaptive Server Enterprise};Server=$server;Database=$dbname;";
break;
default:
throw new Exception("Unsupported database driver: $driver");
}
try {
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
return null;
}
}
$connection_pdo = getPDOConnection();
?>