forked from manuerumx/php2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqli.class.php
183 lines (181 loc) · 4.89 KB
/
mysqli.class.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Class used for handle MySQL connections for servers using versions 4.01 or higher
* @package PHP2HTML
* @subpackage databases
* @version 1.0 BETA
* @author MANUEL GONZALEZ RIVERA <[email protected]>
* @copyright Copyright (R) 2012, MANUEL GONZALEZ RIVERA <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
*
*/
/**
* SQLFunctions class is needed
* @see SQLFunctions
*/
require_once 'sqlfunctions.class.php';
/**
* Class used for handle MySQL connections for servers using versions 4.01 or higher
*
*
* @package PHP2HTML
* @subpackage databases
* @version 1.0
* @author MANUEL GONZALEZ RIVERA <[email protected]>
* @copyright Copyright (R) 2012, MANUEL GONZALEZ RIVERA <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
*
*/
class mysqliConn extends SQLFunctions{
/**
* Initialize the class
*
* @param $host string
* @param $database string
* @param $user string
* @param $password string
* @param $persistant boolean
*/
public function __construct($obj, $host =DB_HOST, $database = DB_DATABASE, $user = DB_USER, $password = DB_PASS, $persistant = DB_PERSIST) {
$this->HtmlC = $obj;
$this->host = $host;
$this->database = $database;
$this->user = $user;
$this->password = $password;
$this->persistant = $persistant;
$this->Conn();
}
/**
* Open the connection
*
*/
protected function Conn() {
try{
if($this->persistant == true){
$this->conn = mysqli_connect('p:'.$this->host, $this->user, $this->password, $this->database);
}else{
$this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->database);
}
if(!$this->conn){
$this->HtmlC->display_error('mysqliConn:Conn()', mysqli_connect_error());
}
}catch(Exception $e){
$this->HtmlC->display_error('mysqliConn:Conn()',$e->getMessage());
}
}
/**
* Get the connection info
*
* return string
*/
public function ConnSummary() {
return $this->user.'@'.$this->database.':'.$this->host;
}
/**
* Returns an associative array with the following record
*
* @param boolean $assoc
* @return mixed
*/
public function Fetch($assoc = true){
if($assoc == true){
$this->row = mysqli_fetch_assoc($this->rs);
}else{
$this->row = mysqli_fetch_array($this->rs);
}
return is_array($this->row);
}
/**
* Try running a SQL query. If the parameter is empty, it takes the variable $this->sql
*
* @param string $sql Sql Query
*/
public function Query($sql = '') {
if(is_resource($this->rs)) {
mysqli_free_result($this->rs);
}
$this->sql = ($sql=="" ? $this->sql : $sql);
$this->rs = mysqli_query($this->conn,$this->sql);
}
/**
* Move the pointer to the index consultation indicated
*
* @param int $num
* @return boolean
*/
public function Seek($num = 0){
if(!empty($this->rs)) {
$this->row = mysqli_data_seek($this->rs, $num);
return true;
}
return false;
}
/**
* Returns the number of rows affected by a query update, insert or delete
*
* @return int
*/
public function affectedRows() {
if(!empty($this->rs)){
return mysqli_affected_rows($this->conn);
}else{
return 0;
}
}
/**
* Return the last id inserted
*
* @return int
*/
public function getInsertedId() {
if(!empty($this->rs)){
return mysqli_insert_id($this->conn);
}else{
return 0;
}
}
/**
* Returns the number of columns in the query result
*
* @return int
* @throws ExceptionRecorset
*/
public function numColumns() {
if(!empty($this->rs)){
return mysqli_num_fields($this->rs);
}else{
return 0;
}
}
/**
* Returns the number of rows for a SELECT query resolved
*
* @return int
*/
public function numRows() {
if(!empty($this->rs)){
return mysqli_num_rows($this->rs);
}else{
return 0;
}
}
/**
* Destroy
*/
public function __destruct() {
$this->Close();
}
/**
* Close the connection
*/
public function Close() {
$type = (is_resource($this->conn) ? get_resource_type($this->conn) : "none");
if(strstr($type,"mysqli")){
mysqli_close($this->conn);
}else{
if($type!='Unknown'){
//
}
}
}
}