Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 Mysql 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
		$this->setDebugMode($debug);
35
 
36
		// check connection id existing and it is a resource
37
		if (defined('SP_DB_CONN_ID') && is_resource(SP_DB_CONN_ID) ) {
38
		    $this->connectionId =  SP_DB_CONN_ID;
39
		} else {
40
 
41
    		// if mysql persistent connection enabled
42
    		if (SP_DB_PERSISTENT_CONNECTION) {
43
    		    $this->connectionId = @mysql_pconnect($dbServer, $dbUser, $dbPassword, true);
44
    		} else {
45
    		    $this->connectionId = @mysql_connect($dbServer, $dbUser, $dbPassword, true);
46
    		}
47
 
48
    		if (!$this->connectionId){
49
    			$this->showError();
50
    			showErrorMsg("<p style='color:red'>Database connection failed!<br>Please check your database settings!</p>");
51
    		} else {
52
    		    $this->selectDatabase($dbName);
53
    		    $this->query( "SET NAMES utf8");
54
    		    define('SP_DB_CONN_ID', $this->connectionId);
55
    		}
56
		}
57
 
58
	}
59
 
60
	# func to select database
61
	function selectDatabase($dbName){
62
		$res = @mysql_select_db($dbName, $this->connectionId);
63
		$this->showError();
64
		if(mysql_errno() != 0){
65
			showErrorMsg("<p style='color:red'>Database connection failed!<br>Please check your database settings!</p>");
66
		}
67
		return $res;
68
	}
69
 
70
	#func to execute a select query
71
	function select($query, $fetchFirst = false){
72
		$res = mysql_query($query, $this->connectionId);
73
		$this->showError();
74
		if (!$res){
75
			return false;
76
		}
77
		$returnArr = array();
78
		while ($row = mysql_fetch_assoc($res)){
79
			$returnArr[] = $row;
80
		}
81
		mysql_free_result($res);
82
		if ($fetchFirst){
83
			return $returnArr[0];
84
		}
85
		return $returnArr;
86
	}
87
 
88
	# func to Execute a general mysql query
89
	function query($query, $noRows=false){
90
		$res = @mysql_query($query, $this->connectionId);
91
		if ($res){
92
			$this->rowsAffected = @mysql_affected_rows($this->connectionId);
93
			$this->lastInsertId = @mysql_insert_id($this->connectionId);
94
		}else{
95
			$this->showError();
96
			@mysql_free_result($res);
97
		}
98
		if($noRows) $this->noRows = mysql_num_rows($res);
99
		return $res;
100
	}
101
 
102
	# func to get max id of a table
103
	function getMaxId($table, $col='id') {
104
		$sql = "select max($col) maxid from $table";
105
		$maxInfo = $this->select($sql);
106
		return empty($maxInfo[0]['maxid']) ? 1 : $maxInfo[0]['maxid'];
107
	}
108
 
109
	# func to Sets the dubug mode for mysql access
110
	function setDebugMode($debug){
111
		$this->debugMode = $debug ? true: false;
112
		return;
113
	}
114
 
115
	# func to Display the Mysql error
116
	function showError(){
117
		if ($this->debugMode && @mysql_errno() != 0) {
118
			echo "Script Halted. \n Mysql Error Number: " . @mysql_errno() . "\n" . @mysql_error();
119
			$this->close();
120
			exit();
121
		}
122
		return;
123
	}
124
 
125
	# func to escape mysql string
126
	function escapeMysqlString($str){
127
		return mysql_escape_string($str);
128
	}
129
 
130
	# func to Close Mysql Connection
131
	function close(){
132
		$res = @mysql_close($this->connectionId);
133
		return $res;
134
	}
135
 
136
	function importDatabaseFile($filename){
137
 
138
		# temporary variable, used to store current query
139
		$tmpline = '';
140
 
141
		# read in entire file
142
		$lines = file($filename);
143
 
144
		# loop through each line
145
		foreach ($lines as $line){
146
 
147
			# skip it if it's a comment
148
			if (substr($line, 0, 2) == '--' || $line == '')
149
				continue;
150
 
151
			# add this line to the current segment
152
			$tmpline .= $line;
153
 
154
			# if it has a semicolon at the end, it's the end of the query
155
			if (substr(trim($line), -1, 1) == ';'){
156
 
157
				if(!empty($tmpline)){
158
					$this->query($tmpline);
159
				}
160
				$tmpline = '';
161
			}
162
		}
163
 
164
	}
165
}
166
?>