[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 3 /* 4 # ***** BEGIN LICENSE BLOCK ***** 5 # Version: MPL 1.1/GPL 2.0/LGPL 2.1 6 # 7 # The contents of this file are subject to the Mozilla Public License Version 8 # 1.1 (the "License"); you may not use this file except in compliance with 9 # the License. You may obtain a copy of the License at 10 # http://www.mozilla.org/MPL/ 11 # 12 # Software distributed under the License is distributed on an "AS IS" basis, 13 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 14 # for the specific language governing rights and limitations under the 15 # License. 16 # 17 # The Original Code is DotClear Weblog. 18 # 19 # The Initial Developer of the Original Code is 20 # Olivier Meunier. 21 # Portions created by the Initial Developer are Copyright (C) 2003 22 # the Initial Developer. All Rights Reserved. 23 # 24 # Contributor(s): 25 # Loic d'Anterroches 26 # 27 # Alternatively, the contents of this file may be used under the terms of 28 # either the GNU General Public License Version 2 or later (the "GPL"), or 29 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 30 # in which case the provisions of the GPL or the LGPL are applicable instead 31 # of those above. If you wish to allow use of your version of this file only 32 # under the terms of either the GPL or the LGPL, and not to allow others to 33 # use your version of this file under the terms of the MPL, indicate your 34 # decision by deleting the provisions above and replace them with the notice 35 # and other provisions required by the GPL or the LGPL. If you do not delete 36 # the provisions above, a recipient may use your version of this file under 37 # the terms of any one of the MPL, the GPL or the LGPL. 38 # 39 # ***** END LICENSE BLOCK ***** */ 40 41 42 43 require_once dirname(__FILE__).'/class.recordset.php'; 44 45 /** 46 * MySQL connection class 47 */ 48 class Connection 49 { 50 var $con_id; 51 var $error; 52 var $errno; 53 var $pfx = ''; 54 var $debug = false; 55 /** The last query, set with debug(). Used when an error is returned. */ 56 var $lastquery = ''; 57 58 function Connection($user, $pwd, $alias='', $dbname, $pfx='', $debug=false, $version='4.0') 59 { 60 $this->error = ''; 61 $this->con_id = @mysql_connect($alias, $user, $pwd); 62 $this->debug = $debug; 63 $this->pfx = $pfx; 64 $this->debug('* MYSQL CONNECT'); 65 if (!$this->con_id) { 66 $this->setError(); 67 } else { 68 $this->database($dbname); 69 } 70 if (strlen($version) and version_compare($version, '4.1', '>=')) { 71 $this->execute('SET NAMES \'utf8\''); 72 } 73 } 74 75 76 function database($dbname) 77 { 78 $db = @mysql_select_db($dbname); 79 $this->debug('* USE DATABASE '.$dbname); 80 if (!$db) { 81 $this->setError(); 82 return false; 83 } else { 84 return true; 85 } 86 } 87 88 /** 89 * Log the queries. Keep track of the last query and if in debug mode 90 * keep track of all the queries in 91 * $GLOBALS['_PX_debug_data']['sql_queries'] 92 * 93 * @param string Query to keep track 94 * @return bool true 95 */ 96 function debug($query) 97 { 98 $this->lastquery = $query; 99 if (!$this->debug) return true; 100 if (!isset($GLOBALS['_PX_debug_data']['sql_queries'])) 101 $GLOBALS['_PX_debug_data']['sql_queries'] = array(); 102 $GLOBALS['_PX_debug_data']['sql_queries'][] = $query; 103 return true; 104 } 105 106 /** 107 * Get results from the cache 108 * 109 * @param string Query 110 * @return mixed Array or false if not cached 111 */ 112 function getFromCache($query) 113 { 114 if (!isset($this->cached_res[md5($query)])) { 115 return false; 116 } 117 return $this->cached_res[md5($query)]; 118 } 119 120 /** 121 * Cache the results 122 * 123 * @param string Query 124 * @param array Results 125 * @return bool true 126 */ 127 function setInCache($query, $res) 128 { 129 $this->cached_res[md5($query)] = $res; 130 return true; 131 } 132 133 134 function close() 135 { 136 if ($this->con_id) { 137 mysql_close($this->con_id); 138 return true; 139 } else { 140 return false; 141 } 142 } 143 144 145 function select($query, $class='recordset') 146 { 147 if (!$this->con_id) { 148 return false; 149 } 150 151 if ($class == '' || !class_exists($class)) { 152 $class = 'recordset'; 153 } 154 $this->debug($query); 155 $cur = mysql_unbuffered_query($query, $this->con_id); 156 157 if ($cur) { 158 // Insertion dans le reccordset 159 $i = 0; 160 $arryRes = array(); 161 while ($res = mysql_fetch_row($cur)) { 162 for ($j=0; $j<count($res); $j++) { 163 $arryRes[$i][strtolower(mysql_field_name($cur, $j))] = $res[$j]; 164 } 165 $i++; 166 } 167 if (func_num_args() == 4) { 168 $_arg = func_get_arg(2); 169 $_arg1 = func_get_arg(3); 170 return new $class($arryRes, $_arg, $_arg1); 171 } 172 return new $class($arryRes); 173 } else { 174 $this->setError(); 175 return false; 176 } 177 } 178 179 function execute($query) 180 { 181 if (!$this->con_id) { 182 return false; 183 } 184 $this->cached_res = array(); 185 $this->debug($query); 186 $cur = mysql_query($query, $this->con_id); 187 if (!$cur) { 188 $this->setError(); 189 return false; 190 } else { 191 return true; 192 } 193 } 194 195 196 function getLastID() 197 { 198 if ($this->con_id) { 199 $this->debug('* GET LAST ID'); 200 return mysql_insert_id($this->con_id); 201 } else { 202 return false; 203 } 204 } 205 206 function setError() 207 { 208 if ($this->con_id) { 209 $this->error = mysql_error($this->con_id).' - '.$this->lastquery; 210 $this->errno = mysql_errno($this->con_id); 211 } else { 212 $this->error = mysql_error().' - '.$this->lastquery; 213 $this->errno = mysql_errno(); 214 } 215 } 216 217 function error() 218 { 219 if ($this->error != '') { 220 return $this->errno.' - '.$this->error; 221 } else { 222 return false; 223 } 224 } 225 226 function escapeStr($str) 227 { 228 return mysql_escape_string($str); 229 } 230 231 function esc($str) 232 { 233 return mysql_escape_string($str); 234 } 235 } 236 237 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |