-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe_connection.php
executable file
·95 lines (70 loc) · 2.92 KB
/
the_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
95
<?php
/*
* Author: Ian Innocent
* For: The Connection
*/
@session_start();
class connection{
public $db_name; public $db_host; public $db_username;
public $db_passwd;
//The class constructor
public function __construct($db_name, $db_host, $db_username, $db_passwd){
if(@$db_host != '' && @$db_name != '' && @$db_username != ''){
$this->db_host = @$db_host;
$this->db_name = @$db_name;
$this->db_passwd = @$db_passwd;
$this->db_username = @$db_username;
$this->db_connect();
}else{
die ('<div class="alert alert-danger alert-bold-border fade in alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error!</strong><br> Could Not Recognize Database connection Criteria <a href="index.php" class="alert-link">Try Again</a>.
</div>
');
}
}
// The database connector!
public final function db_connect(){
$this->con = mysqli_connect($this->db_host,$this->db_username,$this->db_passwd,$this->db_name);
if (mysqli_connect_errno($this->con)){
echo '<div class="alert alert-danger alert-bold-border fade in alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error!</strong><br> Mysqli Error Encountered! <a href="#" class="alert-link">'.mysqli_connect_error().'</a>.
</div>';
exit;
}
}
public function die_on_err($stops){
if(@$stops){
$_SESSION['query_error'] = array();
$_SESSION['query_error'][] = mysqli_error($this->con);
echo '<div class="alert alert-danger alert-bold-border fade in alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error!</strong><br> Mysqli Error Encountered! <a href="#" class="alert-link">'.json_encode(mysqli_error($this->con)).'</a>.
</div>';
exit;
}else{
if(!isset($_SESSION['query_error'])){$_SESSION['query_error'] = array();}
$_SESSION['query_error'][] = mysqli_error($this->con);
}
}
public function query($statement, $stops=false ){
$_SESSION['query'] = mysqli_query($this->con,"$statement")or $this->die_on_err($stops);
return $_SESSION['query'];
}
public function num_rows($statement, $stops=false ){
$this->query($statement, $stops);
$stmt = $_SESSION['query'];
$_SESSION['num_rows'] = mysqli_num_rows( $stmt );
return $_SESSION['num_rows'];
}
public function printQueryResults( $statement, $stops=false ){
$this->query($statement, $stops);
$resArray = array();
while( $response = mysqli_fetch_assoc( $_SESSION['query'] ) ){
$resArray[] = $response;
}
return $resArray;
}
//End of Class
}