-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connect.php
More file actions
106 lines (84 loc) · 2.62 KB
/
db_connect.php
File metadata and controls
106 lines (84 loc) · 2.62 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
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
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
private $g_link = false;
private $CON;
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
function getConn(){
return $this->CON;
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/include/db_info.php';
global $g_link;
global $CON;
if( $g_link )
return $this->CON;
// Connecting to mysql database
$this->CON = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysqli_connect_error());
//$CON = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
//$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// Selecing database
//$db =
//mysqli_select_db(DB_DATABASE) or die(mysql_error());
$g_link = true;
// returing connection cursor
return $this->CON;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
global $g_link;
global $CON;
if ($g_link){
mysqli_close($this->CON);
$g_link = false;
}
}
function fetch($result)
{
$array = array();
if($result instanceof mysqli_stmt)
{
$result->store_result();
$variables = array();
$data = array();
$meta = $result->result_metadata();
while($field = $meta->fetch_field())
$variables[] = &$data[$field->name]; // pass by reference
call_user_func_array(array($result, 'bind_result'), $variables);
$i=0;
while($result->fetch())
{
$array[$i] = array();
foreach($data as $k=>$v)
$array[$i][$k] = $v;
$i++;
// don't know why, but when I tried $array[] = $data, I got the same one result in all rows
}
}
elseif($result instanceof mysqli_result)
{
while($row = $result->fetch_assoc())
$array[] = $row;
}
return $array;
}
}
?>