| 155 |
- |
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 database functions
|
|
|
24 |
class DBI{
|
|
|
25 |
|
|
|
26 |
var $connectionId = false; # db connectio id
|
|
|
27 |
var $error = false; # error while databse operations
|
|
|
28 |
|
|
|
29 |
function connectDatabase($dbServer, $dbUser, $dbPassword, $dbName){
|
|
|
30 |
|
|
|
31 |
$this->connectionId = @mysqli_connect($dbServer, $dbUser, $dbPassword, $dbName);
|
|
|
32 |
|
|
|
33 |
if (!$this->connectionId){
|
|
|
34 |
$this->error = true;
|
|
|
35 |
$error = "Mysql Error: Database connection failed.";
|
|
|
36 |
return $error;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
return true;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
# func to Execute a general mysql query
|
|
|
43 |
function query($query, $noRows=false){
|
|
|
44 |
|
|
|
45 |
$res = @mysqli_query($this->connectionId, $query);
|
|
|
46 |
|
|
|
47 |
if (empty($res)){
|
|
|
48 |
return $this->getError();
|
|
|
49 |
@mysqli_free_result($res);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
return $res;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
#func to execute a select query
|
|
|
56 |
function select($query, $fetchFirst = false){
|
|
|
57 |
$res = @mysqli_query($this->connectionId, $query);
|
|
|
58 |
if (!$res){
|
|
|
59 |
return false;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$returnArr = array();
|
|
|
63 |
while ($row = mysqli_fetch_assoc($res)){
|
|
|
64 |
$returnArr[] = $row;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
mysqli_free_result($res);
|
|
|
68 |
if ($fetchFirst){
|
|
|
69 |
return $returnArr[0];
|
|
|
70 |
}
|
|
|
71 |
return $returnArr;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
# func to Display the Mysql error
|
|
|
75 |
function getError(){
|
|
|
76 |
if (@mysqli_errno($this->connectionId) != 0) {
|
|
|
77 |
$this->error = true;
|
|
|
78 |
$error = "Mysql Error: " . @mysqli_error($this->connectionId);
|
|
|
79 |
}
|
|
|
80 |
return $error;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
function importDatabaseFile($filename, $block=true){
|
|
|
84 |
|
|
|
85 |
# temporary variable, used to store current query
|
|
|
86 |
$tmpline = '';
|
|
|
87 |
|
|
|
88 |
# read in entire file
|
|
|
89 |
$lines = file($filename);
|
|
|
90 |
|
|
|
91 |
# loop through each line
|
|
|
92 |
foreach ($lines as $line){
|
|
|
93 |
|
|
|
94 |
# skip it if it's a comment
|
|
|
95 |
if (substr($line, 0, 2) == '--' || $line == '')
|
|
|
96 |
continue;
|
|
|
97 |
|
|
|
98 |
# add this line to the current segment
|
|
|
99 |
$tmpline .= $line;
|
|
|
100 |
|
|
|
101 |
# if it has a semicolon at the end, it's the end of the query
|
|
|
102 |
if (substr(trim($line), -1, 1) == ';'){
|
|
|
103 |
|
|
|
104 |
if(!empty($tmpline)){
|
|
|
105 |
$errMsg = $this->query($tmpline);
|
|
|
106 |
if($block && $this->error) return $errMsg;
|
|
|
107 |
}
|
|
|
108 |
$tmpline = '';
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
?>
|