[1] | 1 | <?php |
---|
| 2 | /*********************************************************************** |
---|
| 3 | |
---|
| 4 | Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) |
---|
| 5 | |
---|
| 6 | This file is part of PunBB. |
---|
| 7 | |
---|
| 8 | PunBB is free software; you can redistribute it and/or modify it |
---|
| 9 | under the terms of the GNU General Public License as published |
---|
| 10 | by the Free Software Foundation; either version 2 of the License, |
---|
| 11 | or (at your option) any later version. |
---|
| 12 | |
---|
| 13 | PunBB is distributed in the hope that it will be useful, but |
---|
| 14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | GNU General Public License for more details. |
---|
| 17 | |
---|
| 18 | You should have received a copy of the GNU General Public License |
---|
| 19 | along with this program; if not, write to the Free Software |
---|
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
| 21 | MA 02111-1307 USA |
---|
| 22 | |
---|
| 23 | ************************************************************************/ |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | // Make sure we have built in support for MySQL |
---|
| 27 | if (!function_exists('mysql_connect')) |
---|
| 28 | exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.'); |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | class DBLayer |
---|
| 32 | { |
---|
| 33 | var $prefix; |
---|
| 34 | var $link_id; |
---|
| 35 | var $query_result; |
---|
| 36 | |
---|
| 37 | var $saved_queries = array(); |
---|
| 38 | var $num_queries = 0; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) |
---|
| 42 | { |
---|
| 43 | $this->prefix = $db_prefix; |
---|
| 44 | |
---|
| 45 | if ($p_connect) |
---|
| 46 | $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password); |
---|
| 47 | else |
---|
| 48 | $this->link_id = @mysql_connect($db_host, $db_username, $db_password); |
---|
| 49 | |
---|
| 50 | if ($this->link_id) |
---|
| 51 | { |
---|
| 52 | if (@mysql_select_db($db_name, $this->link_id)) |
---|
| 53 | { mysql_query("SET NAMES utf8"); |
---|
| 54 | return $this->link_id; |
---|
| 55 | } |
---|
| 56 | else |
---|
| 57 | error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
---|
| 58 | } |
---|
| 59 | else |
---|
| 60 | error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | function start_transaction() |
---|
| 65 | { |
---|
| 66 | return; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | function end_transaction() |
---|
| 71 | { |
---|
| 72 | return; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | function query($sql, $unbuffered = false) |
---|
| 77 | { |
---|
| 78 | if (defined('PUN_SHOW_QUERIES')) |
---|
| 79 | $q_start = get_microtime(); |
---|
| 80 | |
---|
| 81 | if ($unbuffered) |
---|
| 82 | $this->query_result = @mysql_unbuffered_query($sql, $this->link_id); |
---|
| 83 | else |
---|
| 84 | $this->query_result = @mysql_query($sql, $this->link_id); |
---|
| 85 | |
---|
| 86 | if ($this->query_result) |
---|
| 87 | { |
---|
| 88 | if (defined('PUN_SHOW_QUERIES')) |
---|
| 89 | $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
---|
| 90 | |
---|
| 91 | ++$this->num_queries; |
---|
| 92 | |
---|
| 93 | return $this->query_result; |
---|
| 94 | } |
---|
| 95 | else |
---|
| 96 | { |
---|
| 97 | if (defined('PUN_SHOW_QUERIES')) |
---|
| 98 | $this->saved_queries[] = array($sql, 0); |
---|
| 99 | |
---|
| 100 | return false; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | function result($query_id = 0, $row = 0) |
---|
| 106 | { |
---|
| 107 | return ($query_id) ? @mysql_result($query_id, $row) : false; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | function fetch_assoc($query_id = 0) |
---|
| 112 | { |
---|
| 113 | return ($query_id) ? @mysql_fetch_assoc($query_id) : false; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | function fetch_row($query_id = 0) |
---|
| 118 | { |
---|
| 119 | return ($query_id) ? @mysql_fetch_row($query_id) : false; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | function num_rows($query_id = 0) |
---|
| 124 | { |
---|
| 125 | return ($query_id) ? @mysql_num_rows($query_id) : false; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | function affected_rows() |
---|
| 130 | { |
---|
| 131 | return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | function insert_id() |
---|
| 136 | { |
---|
| 137 | return ($this->link_id) ? @mysql_insert_id($this->link_id) : false; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | function get_num_queries() |
---|
| 142 | { |
---|
| 143 | return $this->num_queries; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | function get_saved_queries() |
---|
| 148 | { |
---|
| 149 | return $this->saved_queries; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | function free_result($query_id = false) |
---|
| 154 | { |
---|
| 155 | return ($query_id) ? @mysql_free_result($query_id) : false; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | function escape($str) |
---|
| 160 | { |
---|
| 161 | if (function_exists('mysql_real_escape_string')) |
---|
| 162 | return mysql_real_escape_string($str, $this->link_id); |
---|
| 163 | else |
---|
| 164 | return mysql_escape_string($str); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | function error() |
---|
| 169 | { |
---|
| 170 | $result['error_sql'] = @current(@end($this->saved_queries)); |
---|
| 171 | $result['error_no'] = @mysql_errno($this->link_id); |
---|
| 172 | $result['error_msg'] = @mysql_error($this->link_id); |
---|
| 173 | |
---|
| 174 | return $result; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | function close() |
---|
| 179 | { |
---|
| 180 | if ($this->link_id) |
---|
| 181 | { |
---|
| 182 | if ($this->query_result) |
---|
| 183 | @mysql_free_result($this->query_result); |
---|
| 184 | |
---|
| 185 | return @mysql_close($this->link_id); |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | return false; |
---|
| 189 | } |
---|
| 190 | } |
---|