[239] | 1 | <?php
|
---|
| 2 | /***************************************************************************
|
---|
| 3 | * Package : MySQL Database Class
|
---|
| 4 | * Version : 1.0
|
---|
| 5 | * Date : 05/10/2009
|
---|
| 6 | * Copyright : (C) 2009 Studio77D
|
---|
| 7 | * Author : Rostislav Stoyanov
|
---|
| 8 | * Email : stoyanovrr@gmail.com
|
---|
| 9 | * Site : http://www.studio77d.com/
|
---|
| 10 | * File : db.class.php
|
---|
| 11 | * Usage : http://www.2smart4you.net/php-mysql-database-connection-class/
|
---|
| 12 | *
|
---|
| 13 | * License:
|
---|
| 14 | * This class is dual-licensed under the GNU General Public License and the MIT License and
|
---|
| 15 | * is copyright (C) 2009 Studio77D.
|
---|
| 16 | *
|
---|
| 17 | * This program is free software: you can redistribute it and/or modify
|
---|
| 18 | * it under the terms of the GNU General Public License as published by
|
---|
| 19 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 20 | * (at your option) any later version.
|
---|
| 21 | *
|
---|
| 22 | * This program is distributed in the hope that it will be useful,
|
---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 25 | * GNU General Public License for more details.
|
---|
| 26 | *
|
---|
| 27 | * You should have received a copy of the GNU General Public License
|
---|
| 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 29 | ***************************************************************************/
|
---|
| 30 |
|
---|
| 31 | class DBConnection {
|
---|
| 32 | /**
|
---|
| 33 | * Connection link.
|
---|
| 34 | * @var string
|
---|
| 35 | */
|
---|
| 36 | private $connection;
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Keeps the info of the last used connection.
|
---|
| 40 | * @var string
|
---|
| 41 | */
|
---|
| 42 | private $last_connection=null;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Keeps the info of the last used MySQL query.
|
---|
| 46 | * @var string
|
---|
| 47 | */
|
---|
| 48 | private $msql='';
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Returns the text of the error message from last MySQL operation.
|
---|
| 52 | * @var string
|
---|
| 53 | */
|
---|
| 54 | private $error='';
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * Returns the numerical value of the error message from last MySQL operation.
|
---|
| 58 | * @var integer
|
---|
| 59 | */
|
---|
| 60 | private $errno='';
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Is there any locked tables right now?
|
---|
| 64 | * @var boolean
|
---|
| 65 | */
|
---|
| 66 | private $is_locked=false;
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * The Constructor. Initializes a database connection and selects database.
|
---|
| 70 | * @param string Database host
|
---|
| 71 | * @param string Database username
|
---|
| 72 | * @param string Database password
|
---|
| 73 | * @param string Database name
|
---|
| 74 | *
|
---|
| 75 | * @return boolean
|
---|
| 76 | */
|
---|
| 77 | function DBConnection($db_host='', $db_user='', $db_pass='', $db_name='') {
|
---|
| 78 | $this->connection=mysql_connect($db_host, $db_user, $db_pass);
|
---|
| 79 |
|
---|
| 80 | if ($this->connection){
|
---|
| 81 | if (mysql_select_db($db_name, $this->connection)){
|
---|
| 82 | $this->last_connection=&$this->connection;
|
---|
| 83 | return $this->connection;
|
---|
| 84 | }else{ // if we can't select the database
|
---|
| 85 | $this->display_errors('- Could not select database: '.$db_name.'');
|
---|
| 86 | return false;
|
---|
| 87 | }
|
---|
| 88 | }else{ // if we couldn't connect to the database
|
---|
| 89 | $this->display_errors('- Could not connect to database: '.$db_name.'');
|
---|
| 90 | return false;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Send a MySQL query.
|
---|
| 96 | * @param string Query to run
|
---|
| 97 | * @return mixed
|
---|
| 98 | */
|
---|
| 99 | function rq($msql) {
|
---|
| 100 | $this->last_connection=&$this->connection;
|
---|
| 101 | $this->msql=&$msql;
|
---|
| 102 | $result=mysql_query($msql, $this->connection);
|
---|
| 103 | if ($result){
|
---|
| 104 | $this->queries_count++;
|
---|
| 105 | return $result;
|
---|
| 106 | }else{
|
---|
| 107 | $this->display_errors();
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * Fetch a result row as an associative array.
|
---|
| 114 | * @param string The query which we send.
|
---|
| 115 | * @return array
|
---|
| 116 | */
|
---|
| 117 | function fetch($query) {
|
---|
| 118 | return mysql_fetch_assoc($query);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * Fetch a result row as an object
|
---|
| 123 | * @param string The query which we send.
|
---|
| 124 | * @return array
|
---|
| 125 | */
|
---|
| 126 | function ofetch($query) {
|
---|
| 127 | return mysql_fetch_object($query);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Fetch a result row as an associative array, a numeric array, or both.
|
---|
| 132 | * @param string The query which we send.
|
---|
| 133 | * @return array
|
---|
| 134 | */
|
---|
| 135 | function afetch($query) {
|
---|
| 136 | return mysql_fetch_array($query);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Returns the number of rows from the executed query.
|
---|
| 141 | * @param string The query result.
|
---|
| 142 | * @return integer
|
---|
| 143 | */
|
---|
| 144 | function num_rows($result) {
|
---|
| 145 | return mysql_num_rows($result);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Retuns the number of rows affected bt last used query.
|
---|
| 150 | * @return integer
|
---|
| 151 | */
|
---|
| 152 | function affected_rows() {
|
---|
| 153 | return mysql_affected_rows($this->last_connection);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Returns the total number of executed queries. Usually goes to the end of scripts.
|
---|
| 158 | * @return integer
|
---|
| 159 | */
|
---|
| 160 | function num_queries() {
|
---|
| 161 | return $this->queries_count;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * Lock database table(s).
|
---|
| 166 | * @param array Array of table => Lock type
|
---|
| 167 | * @return void
|
---|
| 168 | */
|
---|
| 169 |
|
---|
| 170 | function lock_tables($tables) {
|
---|
| 171 | if (is_array($tables)&&count($tables)>0){
|
---|
| 172 | $msql='';
|
---|
| 173 |
|
---|
| 174 | foreach ($tables as $name=>$type){
|
---|
| 175 | $msql.=(!empty($msql)?', ':'').''.$name.' '.$type.'';
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | $this->rq('LOCK TABLES '.$msql.'');
|
---|
| 179 | $this->is_locked=true;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* Unlock database table(s) */
|
---|
| 184 | function unlock_tables() {
|
---|
| 185 | if ($this->is_locked){
|
---|
| 186 | $this->rq('UNLOCK TABLES');
|
---|
| 187 | $this->is_locked=false;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /**
|
---|
| 192 | * Returns the last unique ID (auto_increment field) from the last inserted row.
|
---|
| 193 | * @return integer
|
---|
| 194 | */
|
---|
| 195 | function last_id() {
|
---|
| 196 | return mysql_insert_id($this->connection);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * Escapes a value to make it safe for using in queries.
|
---|
| 201 | * @param string String to be escaped
|
---|
| 202 | * @param bool If escaping of % and _ is also needed
|
---|
| 203 | * @return string
|
---|
| 204 | */
|
---|
| 205 | function string_escape($string, $full_escape=false) {
|
---|
| 206 | $string=stripslashes($string);
|
---|
| 207 |
|
---|
| 208 | if ($full_escape) $string=str_replace(array('%', '_'), array('\%', '\_'), $string);
|
---|
| 209 |
|
---|
| 210 | if (function_exists('mysql_real_escape_string')){
|
---|
| 211 | return mysql_real_escape_string($string, $this->connection);
|
---|
| 212 | }else{
|
---|
| 213 | return mysql_escape_string($string);
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * Free result memory.
|
---|
| 219 | * @param string The result which we want to release.
|
---|
| 220 | * @return boolean
|
---|
| 221 | */
|
---|
| 222 | function free_result($result) {
|
---|
| 223 | return mysql_free_result($result);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
| 227 | * Closes the MySQL connection.
|
---|
| 228 | * @param none
|
---|
| 229 | * @return boolean
|
---|
| 230 | */
|
---|
| 231 | function close() {
|
---|
| 232 | $this->msql='';
|
---|
| 233 | return mysql_close($this->connection);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * Returns the MySQL error message.
|
---|
| 238 | * @return string
|
---|
| 239 | */
|
---|
| 240 | function error() {
|
---|
| 241 | $this->error=(is_null($this->last_connection))?'':mysql_error($this->last_connection);
|
---|
| 242 | return $this->error;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * Returns the MySQL error number.
|
---|
| 247 | * @return string
|
---|
| 248 | */
|
---|
| 249 | function errno() {
|
---|
| 250 | $this->errno=(is_null($this->last_connection))?0:mysql_errno($this->last_connection);
|
---|
| 251 | return $this->errno;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * If database error occur, the script will be stopped and an error message displayed.
|
---|
| 256 | * @param string The error message. If it's empty, it will be created with $this->sql.
|
---|
| 257 | * @return string
|
---|
| 258 | */
|
---|
| 259 | function display_errors($error_message='') {
|
---|
| 260 | if ($this->last_connection){
|
---|
| 261 | $this->error=$this->error($this->last_connection);
|
---|
| 262 | $this->errno=$this->errno($this->last_connection);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /** CHECK $_SERVER to add more details **/
|
---|
| 266 | if(!$error_message) $error_message='- Error in query: '.$this->msql;
|
---|
| 267 |
|
---|
| 268 | $message=''.$error_message.'<br />
|
---|
| 269 | '.(($this->errno!='')?'- Error: '.$this->error.' (Error #'.$this->errno.')<br />':'').'
|
---|
| 270 | - File: '.$_SERVER['SCRIPT_FILENAME'].'<br />';
|
---|
| 271 |
|
---|
| 272 | die('Database problem occur, please try again later.<br />'.$message.'');
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /*** Shortcut functions ***/
|
---|
| 276 | /**
|
---|
| 277 | * For fetching single row with possibility to enter conditions
|
---|
| 278 | *
|
---|
| 279 | * @param string From which table(s) to get result
|
---|
| 280 | * @param string What conditions to use
|
---|
| 281 | * @param string Which field to fetch. Very usable if you need to make JOINs
|
---|
| 282 | *
|
---|
| 283 | * @return mixed
|
---|
| 284 | */
|
---|
| 285 | function getRow($table, $conditions='1', $select_what='*') {
|
---|
| 286 | $query='SELECT '.$select_what.' FROM '.$table.' WHERE '.$conditions;
|
---|
| 287 | $res=$this->rq($query);
|
---|
| 288 | $row=$this->fetch($res);
|
---|
| 289 | return $row;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 | ?>
|
---|