-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconexao.php
More file actions
46 lines (37 loc) · 1.06 KB
/
conexao.php
File metadata and controls
46 lines (37 loc) · 1.06 KB
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
<?php
define('DB_HOST', 'BRUNO\SQLEXPRESS');
define('DB_USER', 'bruno');
define('DB_PASSWORD', '123456');
define('DB_NAME', 'bruno');
define('DB_DRIVER', 'sqlsrv');
require_once "Conexao.php";
try {
$Conexao = Conexao::getConnection();
$query = $Conexao->query("SELECT nome, estado,cidade,referencia,descricao FROM cadastro");
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
class Conexao
{
private static $connection;
private function __construct()
{
}
public static function getConnection()
{
$pdoConfig = DB_DRIVER . ":" . "Server=" . DB_HOST . ";";
$pdoConfig .= "Database=" . DB_NAME . ";";
try {
if (!isset($connection)) {
$connection = new PDO($pdoConfig, DB_USER, DB_PASSWORD);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $connection;
} catch (PDOException $e) {
$mensagem = "Drivers disponiveis: " . implode(",", PDO::getAvailableDrivers());
$mensagem .= "\nErro: " . $e->getMessage();
throw new Exception($mensagem);
}
}
}