forked from LasVegasCoder/PHP-Classes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatabaseClass.php
157 lines (120 loc) · 5.24 KB
/
DatabaseClass.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*#! This Class is used to connect to any database engine, mysql, SQL, Oracle, InformxSQL and more
* @Description : DatabaseClass could be used to establish connection in a singleton pattern to connect to any Database Engine,
* extending PDO Object
* @Return : It return PDO::FETCH_ASSOC as an array, if false or 0, is passed to the DatabaseClass::runQuery third parameter or Fetch() since if true or 1 is passed.
* @doInsert : It does insert data to database and return "data inserted" on success or NULL on failure. Message could be customized as well.
* @doRegister : Function does registeration, insert array of data into database and return message upon success or failure.
* @Author : Prince Adeyemi
* @License : Private... I nearly lost my life, so contact me before using any of this code or get ready for court for violation of everything and Intellectual Property!
* @Contact : Email: [email protected], Facebook: facebook.com/MyVegasPrince
*/
//DEFINED('DATABASE_CONNECT') or DIE('YOU ARE NOT AUTHORIZE TO USE THIS DIRECT. YOUR CONNECTING INFORMATION WAS LOGGED.');
if(!class_exists('DatabaseClass')){
Class DatabaseClass extends PDO {
private static $_link = NULL;
/* In a sigleton, it is a good practice to make it's __Construct private so it is not callable. */
public function __Construct(){
//echo 'DatabaseClass called';
}
/* Connect to database function */
public static function _Connect( $dbengine = 'mysql', $host='127.0.0.1', $dbuser, $dbpass, $dbname = NULL ){
if(( DatabaseClass::$_link )===NULL){
/* Setup PDO Database Singleton Object */
try{
$link = new PDO ("$dbengine:host=$host;dbname=" . $dbname, $dbuser, $dbpass );
$link->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$link->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
DatabaseClass::$_link = $link;
if(DatabaseClass::$_link ){
// print "You're Connected! <br >";
//exit;
}
}
catch( PDOException $e) {
//throw new PDOException( $e->getMessage(), (int) $e->getCode() );
print 'Database Error MSG: ' . $e->getMessage() .'<br >Database Error Code: ' . $e->getCode() .'<br >';
}
}else {
// Database is still active, nothing to do;
}
return DatabaseClass::$_link;
}
/* Query function */
public static function runQuery($query, $params=array(), $all=false){
//echo "runing Query : $query";
$stm = DatabaseClass::$_link->prepare($query);
if(!is_array($params)){
echo "<p style='color:red; font-size:1.5em;'>Parameter must be an Array! Check parameter passed to the runQuery function.</p>";
DatabaseClass::$_link = NULL;
//exit();
}
// echo "<pre>" . PRINT_R($params, 1) . "</pre>";
//echo count($params);
foreach( $params as $field => $value ){
$stm->bindValue( $field, $value);
}
$bool = $stm->execute();
if($bool){
$result = (($all)? $stm->fetch() : $stm->fetchAll());
}else {
$result=NULL;
}
DatabaseClass::$_link = NULL;
return $result;
}
/* Registration function */
public static function doRegister($query, $params=array()){
// echo "runing Query : $query";
$stm = DatabaseClass::$_link->prepare($query);
if(!is_array($params)){
echo "<p style='color:red; font-size:1.5em;'>Parameter must be an Array! Check parameter passed to the doRegister function.</p>";
DatabaseClass::$_link = NULL;
//exit();
}
foreach( $params as $field => $value ){
$stm->bindValue( $field, $value);
}
$bool = $stm->execute();
if($bool){
if( $stm->lastinsertid() > 0 ){
// To do : send an email with an activation link to activate new account.
// DatabaseClass::doActivation( $user_id );
$message = "<p style='color:green'>Registered Successfully! <br >Check your email to activate your account.</p>";
}else {
$message = "<p style='color:red'>Registration failed!.<br > Check your input and try again.</p>";
}
DatabaseClass::$_link = NULL;
}else {
$message = "<p style='color:red'>Weird error occured! <br >Data not inserted.</p>";
}
return $message ;
}
/* Registration function */
public static function doInsert($query, $params=array()){
//echo "runing Query : $query";
$stm = DatabaseClass::$_link->prepare($query);
if(!is_array($params)){
print "<p style='color:red; font-size:1.5em;'>Parameter must be an Array! Check parameter passed to the doRegister function.</p>";
DatabaseClass::$_link = NULL;
//exit();
}
foreach( $params as $field => $value ){
$stm->bindValue( $field, $value);
}
$bool = $stm->execute();
if($bool){
if( $stm->lastinsertid() > 0 ){
$message = "<p style='color:green'>Data Successfully inserted into database.</p>";
}else {
$message = "<p style='color:red'>Data NOT inserted!.<br > Check your input data and try again.</p>";
}
DatabaseClass::$_link = NULL;
}else {
$message = "<p style='color:red'>Weird error occured! <br >Data not inserted.</p>";
}
return $message ;
}
} //end of DatabaseClass
} // end of DatabaseClass class checking