-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.php
94 lines (71 loc) · 2.3 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
//Define
define( 'DIRECTORY', '/MEDART' );
define( 'PATH', 'http://' . $_SERVER['SERVER_NAME'] . '' . DIRECTORY );
define( 'PATH_ADMIN', PATH . '/admin' );
define( 'PATH_SHOP', PATH . '/shop' );
define( 'PATH_CUSTOMER', PATH . '/customer' );
class Database extends Exception {
// Setting up variables
private $host = 'localhost';
private $username = 'root';
private $password = '';
private $db = 'audiometry';
private $dbn;
// Defining constructor
public function __construct() {
$this->connect();
}
// Database connection
public function connect() {
try {
//Crearing database source name
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->db;
//Creating object in PDO
$this->dbn = new PDO($dsn, $this->username, $this->password);
$this->dbn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return true;
} catch( PDOException $e ) {
echo '<script type="text/javascript">console.log("' . ' Error: ' . $e->getMessage() . '");</script>';
}
}
public function execute_query( $sql, $array = NULL ) {
try {
$stmnt = $this->dbn->prepare($sql);
$istrue = $stmnt->execute($array);
if( $istrue ) {
return true;
} else {
return false;
}
} catch (PDOException $e) {
echo '<script type="text/javascript">console.log("' . ' Error: ' . $e->getMessage() . '");</script>';
}
}
// cust function new nr added
public function execute_query_return_id( $sql, $array = NULL ) {
try {
$statement = $this->dbn->prepare($sql);
$big_data = $statement->execute($array);
$retunrId = $this->dbn->lastInsertId();
if( $big_data ) {
return $retunrId;
} else {
return false;
}
} catch (PDOException $e) {
echo '<script type="text/javascript">console.log("' . ' Error: ' . $e->getMessage() . '");</script>';
}
}
public function display( $sql, $array = NULL ) {
try {
$stmnt = $this->dbn->prepare($sql);
$stmnt->execute( $array );
return $istrue = $stmnt->fetchAll();
} catch (PDOException $e) {
echo '<script type="text/javascript">console.log("' . ' Error: ' . $e->getMessage() . '");</script>';
}
}
}
?>