[ Index ] |
|
Code source de phpMyAdmin 2.10.3 |
1 <?php 2 /* $Id: mysql.dbi.lib.php 10356 2007-05-08 20:39:33Z cybot_tm $ */ 3 // vim: expandtab sw=4 ts=4 sts=4: 4 5 /** 6 * Interface to the classic MySQL extension 7 */ 8 9 // MySQL client API 10 if (!defined('PMA_MYSQL_CLIENT_API')) { 11 if (function_exists('mysql_get_client_info')) { 12 $client_api = explode('.', mysql_get_client_info()); 13 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2]))); 14 unset($client_api); 15 } else { 16 define('PMA_MYSQL_CLIENT_API', 32332); // always expect the worst... 17 } 18 } 19 20 function PMA_DBI_real_connect($server, $user, $password, $client_flags) { 21 global $cfg; 22 23 if (empty($client_flags)) { 24 if ($cfg['PersistentConnections']) { 25 $link = @mysql_pconnect($server, $user, $password); 26 } else { 27 $link = @mysql_connect($server, $user, $password); 28 } 29 } else { 30 if ($cfg['PersistentConnections']) { 31 $link = @mysql_pconnect($server, $user, $password, $client_flags); 32 } else { 33 $link = @mysql_connect($server, $user, $password, FALSE, $client_flags); 34 } 35 } 36 37 return $link; 38 } 39 40 function PMA_DBI_connect($user, $password, $is_controluser = FALSE) { 41 global $cfg, $php_errormsg; 42 43 $server_port = (empty($cfg['Server']['port'])) 44 ? '' 45 : ':' . $cfg['Server']['port']; 46 47 if (strtolower($cfg['Server']['connect_type']) == 'tcp') { 48 $cfg['Server']['socket'] = ''; 49 } 50 51 $server_socket = (empty($cfg['Server']['socket'])) 52 ? '' 53 : ':' . $cfg['Server']['socket']; 54 55 $client_flags = 0; 56 57 if (PMA_PHP_INT_VERSION >= 40300 && PMA_MYSQL_CLIENT_API >= 32349) { 58 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h 59 // for the case where the client library was not compiled 60 // with --enable-local-infile 61 $client_flags |= 128; 62 } 63 64 /* Optionally compress connection */ 65 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) { 66 $client_flags |= MYSQL_CLIENT_COMPRESS; 67 } 68 69 /* Optionally enable SSL */ 70 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) { 71 $client_flags |= MYSQL_CLIENT_SSL; 72 } 73 74 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? NULL : $client_flags); 75 76 // Retry with empty password if we're allowed to 77 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) { 78 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? NULL : $client_flags); 79 } 80 81 if (empty($link)) { 82 PMA_auth_fails(); 83 } // end if 84 85 PMA_DBI_postConnect($link, $is_controluser); 86 87 return $link; 88 } 89 90 function PMA_DBI_select_db($dbname, $link = null) { 91 if (empty($link)) { 92 if (isset($GLOBALS['userlink'])) { 93 $link = $GLOBALS['userlink']; 94 } else { 95 return FALSE; 96 } 97 } 98 if (PMA_MYSQL_INT_VERSION < 40100) { 99 $dbname = PMA_convert_charset($dbname); 100 } 101 return mysql_select_db($dbname, $link); 102 } 103 104 function PMA_DBI_try_query($query, $link = null, $options = 0) { 105 if (empty($link)) { 106 if (isset($GLOBALS['userlink'])) { 107 $link = $GLOBALS['userlink']; 108 } else { 109 return FALSE; 110 } 111 } 112 if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) { 113 $query = PMA_convert_charset($query); 114 } 115 if ($options == ($options | PMA_DBI_QUERY_STORE)) { 116 return @mysql_query($query, $link); 117 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) { 118 return @mysql_unbuffered_query($query, $link); 119 } else { 120 return @mysql_query($query, $link); 121 } 122 } 123 124 // The following function is meant for internal use only. 125 // Do not call it from outside this library! 126 function PMA_mysql_fetch_array($result, $type = FALSE) { 127 global $cfg, $allow_recoding, $charset, $convcharset; 128 129 if ($type != FALSE) { 130 $data = mysql_fetch_array($result, $type); 131 } else { 132 $data = mysql_fetch_array($result); 133 } 134 135 /* No data returned => do not touch it */ 136 if (! $data) { 137 return $data; 138 } 139 140 if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100 141 || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) { 142 /* No recoding -> return data as we got them */ 143 return $data; 144 } else { 145 $ret = array(); 146 $num = mysql_num_fields($result); 147 $i = 0; 148 for ($i = 0; $i < $num; $i++) { 149 $name = mysql_field_name($result, $i); 150 $flags = mysql_field_flags($result, $i); 151 /* Field is BINARY (either marked manually, or it is BLOB) => do not convert it */ 152 if (stristr($flags, 'BINARY')) { 153 if (isset($data[$i])) { 154 $ret[$i] = $data[$i]; 155 } 156 if (isset($data[$name])) { 157 $ret[PMA_convert_display_charset($name)] = $data[$name]; 158 } 159 } else { 160 if (isset($data[$i])) { 161 $ret[$i] = PMA_convert_display_charset($data[$i]); 162 } 163 if (isset($data[$name])) { 164 $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]); 165 } 166 } 167 } 168 return $ret; 169 } 170 } 171 172 function PMA_DBI_fetch_array($result) { 173 return PMA_mysql_fetch_array($result); 174 } 175 176 function PMA_DBI_fetch_assoc($result) { 177 return PMA_mysql_fetch_array($result, MYSQL_ASSOC); 178 } 179 180 function PMA_DBI_fetch_row($result) { 181 return PMA_mysql_fetch_array($result, MYSQL_NUM); 182 } 183 184 /** 185 * Frees the memory associated with the results 186 * 187 * @param result $result,... one or more mysql result resources 188 */ 189 function PMA_DBI_free_result() { 190 foreach ( func_get_args() as $result ) { 191 if ( is_resource($result) 192 && get_resource_type($result) === 'mysql result' ) { 193 mysql_free_result($result); 194 } 195 } 196 } 197 198 /** 199 * Returns a string representing the type of connection used 200 * @uses mysql_get_host_info() 201 * @uses $GLOBALS['userlink'] as default for $link 202 * @param resource $link mysql link 203 * @return string type of connection used 204 */ 205 function PMA_DBI_get_host_info($link = null) 206 { 207 if (null === $link) { 208 if (isset($GLOBALS['userlink'])) { 209 $link = $GLOBALS['userlink']; 210 } else { 211 return false; 212 } 213 } 214 return mysql_get_host_info($link); 215 } 216 217 /** 218 * Returns the version of the MySQL protocol used 219 * @uses mysql_get_proto_info() 220 * @uses $GLOBALS['userlink'] as default for $link 221 * @param resource $link mysql link 222 * @return integer version of the MySQL protocol used 223 */ 224 function PMA_DBI_get_proto_info($link = null) 225 { 226 if (null === $link) { 227 if (isset($GLOBALS['userlink'])) { 228 $link = $GLOBALS['userlink']; 229 } else { 230 return false; 231 } 232 } 233 return mysql_get_proto_info($link); 234 } 235 236 /** 237 * returns a string that represents the client library version 238 * @uses mysql_get_client_info() 239 * @return string MySQL client library version 240 */ 241 function PMA_DBI_get_client_info() { 242 return mysql_get_client_info(); 243 } 244 245 /** 246 * returns last error message or false if no errors occured 247 * 248 * @uses PMA_MYSQL_INT_VERSION 249 * @uses PMA_convert_display_charset() 250 * @uses PMA_DBI_convert_message() 251 * @uses $GLOBALS['errno'] 252 * @uses $GLOBALS['userlink'] 253 * @uses $GLOBALS['strServerNotResponding'] 254 * @uses $GLOBALS['strSocketProblem'] 255 * @uses mysql_errno() 256 * @uses mysql_error() 257 * @uses defined() 258 * @param resource $link mysql link 259 * @return string|boolean $error or false 260 */ 261 function PMA_DBI_getError($link = null) 262 { 263 $GLOBALS['errno'] = 0; 264 if (null === $link && isset($GLOBALS['userlink'])) { 265 $link =& $GLOBALS['userlink']; 266 267 // Do not stop now. On the initial connection, we don't have a $link, 268 // we don't have a $GLOBALS['userlink'], but we can catch the error code 269 // } else { 270 // return FALSE; 271 } 272 273 if (null !== $link) { 274 $error_number = mysql_errno($link); 275 $error_message = mysql_error($link); 276 } else { 277 $error_number = mysql_errno(); 278 $error_message = mysql_error(); 279 } 280 if (0 == $error_number) { 281 return false; 282 } 283 284 // keep the error number for further check after the call to PMA_DBI_getError() 285 $GLOBALS['errno'] = $error_number; 286 287 if (! empty($error_message)) { 288 $error_message = PMA_DBI_convert_message($error_message); 289 } 290 291 // Some errors messages cannot be obtained by mysql_error() 292 if ($error_number == 2002) { 293 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem']; 294 } elseif ($error_number == 2003 ) { 295 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding']; 296 } elseif (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) { 297 $error = '#' . ((string) $error_number) . ' - ' . $error_message; 298 } else { 299 $error = '#' . ((string) $error_number) . ' - ' . PMA_convert_display_charset($error_message); 300 } 301 return $error; 302 } 303 304 function PMA_DBI_close($link = null) 305 { 306 if (empty($link)) { 307 if (isset($GLOBALS['userlink'])) { 308 $link = $GLOBALS['userlink']; 309 } else { 310 return FALSE; 311 } 312 } 313 return @mysql_close($link); 314 } 315 316 function PMA_DBI_num_rows($result) { 317 if (!is_bool($result)) { 318 return mysql_num_rows($result); 319 } else { 320 return 0; 321 } 322 } 323 324 function PMA_DBI_insert_id($link = null) 325 { 326 if (empty($link)) { 327 if (isset($GLOBALS['userlink'])) { 328 $link = $GLOBALS['userlink']; 329 } else { 330 return FALSE; 331 } 332 } 333 //$insert_id = mysql_insert_id($link); 334 // if the primary key is BIGINT we get an incorrect result 335 // (sometimes negative, sometimes positive) 336 // and in the present function we don't know if the PK is BIGINT 337 // so better play safe and use LAST_INSERT_ID() 338 // 339 // by the way, no problem with mysqli_insert_id() 340 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link); 341 } 342 343 function PMA_DBI_affected_rows($link = null) 344 { 345 if (empty($link)) { 346 if (isset($GLOBALS['userlink'])) { 347 $link = $GLOBALS['userlink']; 348 } else { 349 return FALSE; 350 } 351 } 352 return mysql_affected_rows($link); 353 } 354 355 /** 356 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals) 357 */ 358 function PMA_DBI_get_fields_meta($result) { 359 $fields = array(); 360 $num_fields = mysql_num_fields($result); 361 for ($i = 0; $i < $num_fields; $i++) { 362 $fields[] = PMA_convert_display_charset(mysql_fetch_field($result, $i)); 363 } 364 return $fields; 365 } 366 367 function PMA_DBI_num_fields($result) { 368 return mysql_num_fields($result); 369 } 370 371 function PMA_DBI_field_len($result, $i) { 372 return mysql_field_len($result, $i); 373 } 374 375 function PMA_DBI_field_name($result, $i) { 376 return mysql_field_name($result, $i); 377 } 378 379 function PMA_DBI_field_flags($result, $i) { 380 return PMA_convert_display_charset(mysql_field_flags($result, $i)); 381 } 382 383 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 15:18:20 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |