class.database.php
Gesponsorde koppelingen
PHP script bestanden
Code (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
*
* @date 15 Aug 2012,
* @package Login System
*
*/
Class Database {
/**
* @param string $host,
* @param string $username,
* @param string $password,
* @param string $database,
* @param mixed $result
*/
private
$host,
$username,
$password,
$database,
$result;
/**
* @param object $pdo -> static because otherwhise with each page load
* there will be multiple connections
*/
private
static $pdo = false;
/**
* @param string $host,
* @return void
*/
public function setHost($host) {
$this->host = $host;
}
/**
* @param string $username,
* @return void
*/
public function setUsername($username) {
$this->username = $username;
}
/**
* @param string $password,
* @return void
*/
public function setPassword($password) {
$this->password = $password;
}
/**
* @param string $database,
* @return void
*/
public function setDatabase($database) {
$this->database = $database;
}
/**
* @return void
*/
public function getConnection() {
if(!self::$pdo) {
try {
self::$pdo = new PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
defined('DEBUG') && DEBUG == false ? exit('Database connection failed.') : exit('Database connection error: '.$e->getMessage().'.');
}
}
}
/**
* @param string $qry,
* @param array $args -> can be empty when not using WHERE,
* @return mixed $result/exception
*/
public function query($qry, $args = array()) {
try {
$this->result = $stmt = self::$pdo->prepare($qry);
return $stmt->execute($args);
}
catch(PDOException $e) {
defined('DEBUG') && DEBUG == true ? exit('Query failed: '.$e->getMessage().'.') : exit('Query failed.');
}
}
/**
* @return mixed $rows
*/
public function fetchAll() {
return $this->result->fetchAll(PDO::FETCH_OBJ);
}
/**
* @return mixed $rows
*/
public function fetch() {
return $this->result->fetch(PDO::FETCH_OBJ);
}
/**
* @return int $rows
*/
public function rowCount() {
return count($this->result);
}
}
?>
/**
*
* @date 15 Aug 2012,
* @package Login System
*
*/
Class Database {
/**
* @param string $host,
* @param string $username,
* @param string $password,
* @param string $database,
* @param mixed $result
*/
private
$host,
$username,
$password,
$database,
$result;
/**
* @param object $pdo -> static because otherwhise with each page load
* there will be multiple connections
*/
private
static $pdo = false;
/**
* @param string $host,
* @return void
*/
public function setHost($host) {
$this->host = $host;
}
/**
* @param string $username,
* @return void
*/
public function setUsername($username) {
$this->username = $username;
}
/**
* @param string $password,
* @return void
*/
public function setPassword($password) {
$this->password = $password;
}
/**
* @param string $database,
* @return void
*/
public function setDatabase($database) {
$this->database = $database;
}
/**
* @return void
*/
public function getConnection() {
if(!self::$pdo) {
try {
self::$pdo = new PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
defined('DEBUG') && DEBUG == false ? exit('Database connection failed.') : exit('Database connection error: '.$e->getMessage().'.');
}
}
}
/**
* @param string $qry,
* @param array $args -> can be empty when not using WHERE,
* @return mixed $result/exception
*/
public function query($qry, $args = array()) {
try {
$this->result = $stmt = self::$pdo->prepare($qry);
return $stmt->execute($args);
}
catch(PDOException $e) {
defined('DEBUG') && DEBUG == true ? exit('Query failed: '.$e->getMessage().'.') : exit('Query failed.');
}
}
/**
* @return mixed $rows
*/
public function fetchAll() {
return $this->result->fetchAll(PDO::FETCH_OBJ);
}
/**
* @return mixed $rows
*/
public function fetch() {
return $this->result->fetch(PDO::FETCH_OBJ);
}
/**
* @return int $rows
*/
public function rowCount() {
return count($this->result);
}
}
?>