103 |
- |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/***************************************************************************
|
|
|
4 |
* Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in) *
|
|
|
5 |
* sendtogeo@gmail.com *
|
|
|
6 |
* *
|
|
|
7 |
* This program is free software; you can redistribute it and/or modify *
|
|
|
8 |
* it under the terms of the GNU General Public License as published by *
|
|
|
9 |
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
10 |
* (at your option) any later version. *
|
|
|
11 |
* *
|
|
|
12 |
* This program is distributed in the hope that it will be useful, *
|
|
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
15 |
* GNU General Public License for more details. *
|
|
|
16 |
* *
|
|
|
17 |
* You should have received a copy of the GNU General Public License *
|
|
|
18 |
* along with this program; if not, write to the *
|
|
|
19 |
* Free Software Foundation, Inc., *
|
|
|
20 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
21 |
***************************************************************************/
|
|
|
22 |
|
|
|
23 |
# class defines all mysql functions
|
|
|
24 |
class MysqliHelper extends Database{
|
|
|
25 |
|
|
|
26 |
var $connectionId = false;
|
|
|
27 |
var $debugMode;
|
|
|
28 |
var $rowsAffected;
|
|
|
29 |
var $lastInsertId;
|
|
|
30 |
var $noRows = 0;
|
|
|
31 |
|
|
|
32 |
// constructor
|
|
|
33 |
function __construct($dbServer, $dbUser, $dbPassword, $dbName, $debug){
|
|
|
34 |
global $SP_DB_CONN_OBJ;
|
|
|
35 |
$this->setDebugMode($debug);
|
|
|
36 |
|
|
|
37 |
// check connection id existing and it is a resource
|
|
|
38 |
if (!empty($SP_DB_CONN_OBJ) && is_object($SP_DB_CONN_OBJ) ) {
|
|
|
39 |
$this->connectionId = $SP_DB_CONN_OBJ;
|
|
|
40 |
} else {
|
|
|
41 |
|
|
|
42 |
$dbServer = SP_DB_PERSISTENT_CONNECTION ? "p:$dbServer" : $dbServer;
|
|
|
43 |
$this->connectionId = @mysqli_connect($dbServer, $dbUser, $dbPassword, $dbName);
|
|
|
44 |
|
|
|
45 |
if (!$this->connectionId){
|
|
|
46 |
$this->showError();
|
|
|
47 |
showErrorMsg("<p style='color:red'>Database connection failed!<br>Please check your database settings!</p>");
|
|
|
48 |
} else {
|
|
|
49 |
$this->query( "SET NAMES utf8");
|
|
|
50 |
$SP_DB_CONN_OBJ = $this->connectionId;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
// func to execute a select query
|
|
|
58 |
function select($query, $fetchFirst = false){
|
|
|
59 |
$res = mysqli_query($this->connectionId, $query);
|
|
|
60 |
$this->showError();
|
|
|
61 |
|
|
|
62 |
if (!$res){
|
|
|
63 |
return false;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$returnArr = array();
|
|
|
67 |
while ($row = mysqli_fetch_assoc($res)){
|
|
|
68 |
$returnArr[] = $row;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
mysqli_free_result($res);
|
|
|
72 |
|
|
|
73 |
if ($fetchFirst){
|
|
|
74 |
return $returnArr[0];
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return $returnArr;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// func to Execute a general mysql query
|
|
|
81 |
function query($query, $noRows=false){
|
|
|
82 |
$res = @mysqli_query($this->connectionId, $query);
|
|
|
83 |
|
|
|
84 |
if ($res){
|
|
|
85 |
$this->rowsAffected = @mysqli_affected_rows($this->connectionId);
|
|
|
86 |
$this->lastInsertId = @mysqli_insert_id($this->connectionId);
|
|
|
87 |
} else {
|
|
|
88 |
$this->showError();
|
|
|
89 |
@mysqli_free_result($res);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if($noRows) $this->noRows = mysqli_num_rows($res);
|
|
|
93 |
return $res;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
# func to get max id of a table
|
|
|
97 |
function getMaxId($table, $col='id') {
|
|
|
98 |
$sql = "select max($col) maxid from $table";
|
|
|
99 |
$maxInfo = $this->select($sql);
|
|
|
100 |
return empty($maxInfo[0]['maxid']) ? 1 : $maxInfo[0]['maxid'];
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
# func to Sets the dubug mode for mysql access
|
|
|
104 |
function setDebugMode($debug){
|
|
|
105 |
$this->debugMode = $debug ? true: false;
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
# func to Display the Mysql error
|
|
|
110 |
function showError(){
|
|
|
111 |
|
|
|
112 |
// if debugmode enabled
|
|
|
113 |
if ($this->debugMode) {
|
|
|
114 |
|
|
|
115 |
// if connection is failed
|
|
|
116 |
if (!$this->connectionId) {
|
|
|
117 |
echo "Error: Unable to connect to Database." . PHP_EOL;
|
|
|
118 |
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
|
|
|
119 |
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
|
|
|
120 |
exit;
|
|
|
121 |
} else if (@mysqli_errno($this->connectionId) != 0) {
|
|
|
122 |
echo "Script Halted. \n Mysql Error Number: " . @mysqli_errno($this->connectionId) . "\n" . @mysqli_error($this->connectionId);
|
|
|
123 |
$this->close();
|
|
|
124 |
exit;
|
|
|
125 |
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
# func to escape mysql string
|
|
|
133 |
function escapeMysqlString($str){
|
|
|
134 |
return mysqli_escape_string($this->connectionId, $str);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
# func to Close Mysql Connection
|
|
|
138 |
function close(){
|
|
|
139 |
$res = @mysqli_close($this->connectionId);
|
|
|
140 |
return $res;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
function importDatabaseFile($filename){
|
|
|
144 |
|
|
|
145 |
# temporary variable, used to store current query
|
|
|
146 |
$tmpline = '';
|
|
|
147 |
|
|
|
148 |
# read in entire file
|
|
|
149 |
$lines = file($filename);
|
|
|
150 |
|
|
|
151 |
# loop through each line
|
|
|
152 |
foreach ($lines as $line){
|
|
|
153 |
|
|
|
154 |
# skip it if it's a comment
|
|
|
155 |
if (substr($line, 0, 2) == '--' || $line == '')
|
|
|
156 |
continue;
|
|
|
157 |
|
|
|
158 |
# add this line to the current segment
|
|
|
159 |
$tmpline .= $line;
|
|
|
160 |
|
|
|
161 |
# if it has a semicolon at the end, it's the end of the query
|
|
|
162 |
if (substr(trim($line), -1, 1) == ';'){
|
|
|
163 |
|
|
|
164 |
if(!empty($tmpline)){
|
|
|
165 |
$this->query($tmpline);
|
|
|
166 |
}
|
|
|
167 |
$tmpline = '';
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
?>
|