-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbutil.php
More file actions
34 lines (31 loc) · 1020 Bytes
/
dbutil.php
File metadata and controls
34 lines (31 loc) · 1020 Bytes
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
<?php
/**
* DBUtil
* Contiene métodos utilitarios para acceder a la base de datos
*/
class DBUtil {
/**
* getConnection
*
* Realiza la conexión a la base de datos utilizando la interfaz PDO
*/
public static function getConnection() {
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'example';
$port = 3306;
$dsn = "mysql:host=$server;dbname=$database;port=$port";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
];
try {
$pdo = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
return $pdo;
}
}