Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: mysqli.dbi.lib.php,v 2.37 2005/08/08 14:55:07 lem9 Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00012 if (!@function_exists('mysqli_connect')) { 00013 PMA_dl('mysqli'); 00014 } 00015 00016 // check whether mysql is available 00017 if (!@function_exists('mysqli_connect')) { 00018 require_once('./libraries/header_http.inc.php'); 00019 echo sprintf($strCantLoad, 'mysqli') . '<br />' . "\n" 00020 . '<a href="./Documentation.html#faqmysql" target="documentation">' . $GLOBALS['strDocu'] . '</a>' . "\n"; 00021 exit; 00022 } 00023 00024 // MySQL client API 00025 if (!defined('PMA_MYSQL_CLIENT_API')) { 00026 $client_api = explode('.', mysqli_get_client_info()); 00027 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2]))); 00028 unset($client_api); 00029 } 00030 00031 // Constants from mysql_com.h of MySQL 4.1.3 00032 00033 define('NOT_NULL_FLAG', 1); 00034 define('PRI_KEY_FLAG', 2); 00035 define('UNIQUE_KEY_FLAG', 4); 00036 define('MULTIPLE_KEY_FLAG', 8); 00037 define('BLOB_FLAG', 16); 00038 define('UNSIGNED_FLAG', 32); 00039 define('ZEROFILL_FLAG', 64); 00040 define('BINARY_FLAG', 128); 00041 define('ENUM_FLAG', 256); 00042 define('AUTO_INCREMENT_FLAG', 512); 00043 define('TIMESTAMP_FLAG', 1024); 00044 define('SET_FLAG', 2048); 00045 define('NUM_FLAG', 32768); 00046 define('PART_KEY_FLAG', 16384); 00047 define('UNIQUE_FLAG', 65536); 00048 00049 function PMA_DBI_connect($user, $password, $is_controluser = FALSE) { 00050 global $cfg, $php_errormsg; 00051 00052 $server_port = (empty($cfg['Server']['port'])) 00053 ? FALSE 00054 : (int) $cfg['Server']['port']; 00055 00056 if (strtolower($cfg['Server']['connect_type']) == 'tcp') { 00057 $cfg['Server']['socket'] = ''; 00058 } 00059 00060 // NULL enables connection to the default socket 00061 $server_socket = (empty($cfg['Server']['socket'])) 00062 ? NULL 00063 : $cfg['Server']['socket']; 00064 00065 $link = mysqli_init(); 00066 00067 mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, TRUE); 00068 00069 $client_flags = $cfg['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS') ? MYSQLI_CLIENT_COMPRESS : 0; 00070 00071 $return_value = @mysqli_real_connect($link, $cfg['Server']['host'], $user, $password, FALSE, $server_port, $server_socket, $client_flags); 00072 00073 if ($return_value == FALSE) { 00074 PMA_auth_fails(); 00075 } // end if 00076 00077 PMA_DBI_postConnect($link, $is_controluser); 00078 00079 return $link; 00080 } 00081 00082 function PMA_DBI_select_db($dbname, $link = NULL) { 00083 if (empty($link)) { 00084 if (isset($GLOBALS['userlink'])) { 00085 $link = $GLOBALS['userlink']; 00086 } else { 00087 return FALSE; 00088 } 00089 } 00090 if (PMA_MYSQL_INT_VERSION < 40100) { 00091 $dbname = PMA_convert_charset($dbname); 00092 } 00093 return mysqli_select_db($link, $dbname); 00094 } 00095 00096 function PMA_DBI_try_query($query, $link = NULL, $options = 0) { 00097 if ($options == ($options | PMA_DBI_QUERY_STORE)) { 00098 $method = MYSQLI_STORE_RESULT; 00099 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) { 00100 $method = MYSQLI_USE_RESULT; 00101 } else { 00102 $method = MYSQLI_USE_RESULT; 00103 } 00104 00105 if (empty($link)) { 00106 if (isset($GLOBALS['userlink'])) { 00107 $link = $GLOBALS['userlink']; 00108 } else { 00109 return FALSE; 00110 } 00111 } 00112 if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) { 00113 $query = PMA_convert_charset($query); 00114 } 00115 return mysqli_query($link, $query, $method); 00116 // From the PHP manual: 00117 // "note: returns TRUE on success or FALSE on failure. For SELECT, 00118 // SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a result object" 00119 // so, do not use the return value to feed mysqli_num_rows() if it's 00120 // a boolean 00121 } 00122 00123 // The following function is meant for internal use only. 00124 // Do not call it from outside this library! 00125 function PMA_mysqli_fetch_array($result, $type = FALSE) { 00126 global $cfg, $allow_recoding, $charset, $convcharset; 00127 00128 if ($type != FALSE) { 00129 $data = @mysqli_fetch_array($result, $type); 00130 } else { 00131 $data = @mysqli_fetch_array($result); 00132 } 00133 00134 /* No data returned => do not touch it */ 00135 if (! $data) return $data; 00136 00137 if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100 00138 || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) { 00139 /* No recoding -> return data as we got them */ 00140 return $data; 00141 } else { 00142 $ret = array(); 00143 $num = mysqli_num_fields($result); 00144 if ($num > 0) { 00145 $fields = PMA_DBI_get_fields_meta($result); 00146 } 00147 $i = 0; 00148 for ($i = 0; $i < $num; $i++) { 00149 if (!isset($fields[$i]->type)) { 00150 /* No meta information available -> we guess that it should be converted */ 00151 if (isset($data[$i])) { 00152 $ret[$i] = PMA_convert_display_charset($data[$i]); 00153 } 00154 if (isset($fields[$i]->name) && isset($data[$fields[$i]->name])) { 00155 $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]); 00156 } 00157 } else { 00158 /* Meta information available -> check type of field and convert it according to the type */ 00159 if (stristr($fields[$i]->type, 'BLOB') || stristr($fields[$i]->type, 'BINARY')) { 00160 if (isset($data[$i])) { 00161 $ret[$i] = $data[$i]; 00162 } 00163 if (isset($data[$fields[$i]->name])) { 00164 $ret[PMA_convert_display_charset($fields[$i]->name)] = $data[$fields[$i]->name]; 00165 } 00166 } else { 00167 if (isset($data[$i])) { 00168 $ret[$i] = PMA_convert_display_charset($data[$i]); 00169 } 00170 if (isset($data[$fields[$i]->name])) { 00171 $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]); 00172 } 00173 } 00174 } 00175 } 00176 return $ret; 00177 } 00178 } 00179 00180 function PMA_DBI_fetch_array($result) { 00181 return PMA_mysqli_fetch_array($result, MYSQLI_BOTH); 00182 } 00183 00184 function PMA_DBI_fetch_assoc($result) { 00185 return PMA_mysqli_fetch_array($result, MYSQLI_ASSOC); 00186 } 00187 00188 function PMA_DBI_fetch_row($result) { 00189 return PMA_mysqli_fetch_array($result, MYSQLI_NUM); 00190 } 00191 00192 function PMA_DBI_free_result($result) { 00193 if (!is_bool($result)) { 00194 return mysqli_free_result($result); 00195 } else { 00196 return 0; 00197 } 00198 } 00199 00200 function PMA_DBI_getError($link = NULL) { 00201 unset($GLOBALS['errno']); 00202 if (empty($link)) { 00203 if (isset($GLOBALS['userlink'])) { 00204 $link = $GLOBALS['userlink']; 00205 // Do not stop now. We still can get the error code 00206 // with mysqli_connect_errno() 00207 // } else { 00208 // return FALSE; 00209 } 00210 } 00211 00212 if (mysqli_connect_errno()) { 00213 $error = mysqli_connect_errno(); 00214 $error_message = mysqli_connect_error(); 00215 } elseif ( !empty($link) && mysqli_errno($link)) { 00216 $error = mysqli_errno($link); 00217 $error_message = mysqli_error($link); 00218 } 00219 00220 // keep the error number for further check after the call to PMA_DBI_getError() 00221 if (!empty($error)) { 00222 $GLOBALS['errno'] = $error; 00223 } else { 00224 return FALSE; 00225 } 00226 00227 00228 if ($error && $error == 2002) { 00229 $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem']; 00230 } elseif ($error && defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) { 00231 $error = '#' . ((string) $error) . ' - ' . $error_message; 00232 } elseif ($error) { 00233 $error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset($error_message); 00234 } 00235 return $error; 00236 } 00237 00238 function PMA_DBI_close($link = NULL) { 00239 if (empty($link)) { 00240 if (isset($GLOBALS['userlink'])) { 00241 $link = $GLOBALS['userlink']; 00242 } else { 00243 return FALSE; 00244 } 00245 } 00246 return @mysqli_close($link); 00247 } 00248 00249 function PMA_DBI_num_rows($result) { 00250 // see the note for PMA_DBI_try_query(); 00251 if (!is_bool($result)) { 00252 return @mysqli_num_rows($result); 00253 } else { 00254 return 0; 00255 } 00256 } 00257 00258 function PMA_DBI_insert_id($link = '') { 00259 if (empty($link)) { 00260 if (isset($GLOBALS['userlink'])) { 00261 $link = $GLOBALS['userlink']; 00262 } else { 00263 return FALSE; 00264 } 00265 } 00266 return mysqli_insert_id($link); 00267 } 00268 00269 function PMA_DBI_affected_rows($link = NULL) { 00270 if (empty($link)) { 00271 if (isset($GLOBALS['userlink'])) { 00272 $link = $GLOBALS['userlink']; 00273 } else { 00274 return FALSE; 00275 } 00276 } 00277 return mysqli_affected_rows($link); 00278 } 00279 00280 function PMA_DBI_get_fields_meta($result) { 00281 // Build an associative array for a type look up 00282 $typeAr = Array(); 00283 $typeAr[MYSQLI_TYPE_DECIMAL] = 'real'; 00284 $typeAr[MYSQLI_TYPE_TINY] = 'int'; 00285 $typeAr[MYSQLI_TYPE_SHORT] = 'int'; 00286 $typeAr[MYSQLI_TYPE_LONG] = 'int'; 00287 $typeAr[MYSQLI_TYPE_FLOAT] = 'real'; 00288 $typeAr[MYSQLI_TYPE_DOUBLE] = 'real'; 00289 $typeAr[MYSQLI_TYPE_NULL] = 'null'; 00290 $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp'; 00291 $typeAr[MYSQLI_TYPE_LONGLONG] = 'int'; 00292 $typeAr[MYSQLI_TYPE_INT24] = 'int'; 00293 $typeAr[MYSQLI_TYPE_DATE] = 'date'; 00294 $typeAr[MYSQLI_TYPE_TIME] = 'time'; 00295 $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime'; 00296 $typeAr[MYSQLI_TYPE_YEAR] = 'year'; 00297 $typeAr[MYSQLI_TYPE_NEWDATE] = 'date'; 00298 $typeAr[MYSQLI_TYPE_ENUM] = 'unknown'; 00299 $typeAr[MYSQLI_TYPE_SET] = 'unknown'; 00300 $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob'; 00301 $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob'; 00302 $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob'; 00303 $typeAr[MYSQLI_TYPE_BLOB] = 'blob'; 00304 $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string'; 00305 $typeAr[MYSQLI_TYPE_STRING] = 'string'; 00306 $typeAr[MYSQLI_TYPE_CHAR] = 'string'; 00307 $typeAr[MYSQLI_TYPE_GEOMETRY] = 'unknown'; 00308 00309 $fields = mysqli_fetch_fields($result); 00310 00311 // this happens sometimes (seen under MySQL 4.0.25) 00312 if (!is_array($fields)) { 00313 return FALSE; 00314 } 00315 00316 foreach ($fields as $k => $field) { 00317 $fields[$k]->type = $typeAr[$fields[$k]->type]; 00318 $fields[$k]->flags = PMA_DBI_field_flags($result, $k); 00319 00320 // Enhance the field objects for mysql-extension compatibilty 00321 $flags = explode(' ', $fields[$k]->flags); 00322 array_unshift($flags, 'dummy'); 00323 $fields[$k]->multiple_key = (int)(array_search('multiple_key', $flags, true) > 0); 00324 $fields[$k]->primary_key = (int)(array_search('primary_key', $flags, true) > 0); 00325 $fields[$k]->unique_key = (int)(array_search('unique_key', $flags, true) > 0); 00326 $fields[$k]->not_null = (int)(array_search('not_null', $flags, true) > 0); 00327 $fields[$k]->unsigned = (int)(array_search('unsigned', $flags, true) > 0); 00328 $fields[$k]->zerofill = (int)(array_search('zerofill', $flags, true) > 0); 00329 $fields[$k]->numeric = (int)(array_search('num', $flags, true) > 0); 00330 $fields[$k]->blob = (int)(array_search('blob', $flags, true) > 0); 00331 } 00332 return $fields; 00333 } 00334 00335 function PMA_DBI_num_fields($result) { 00336 return mysqli_num_fields($result); 00337 } 00338 00339 function PMA_DBI_field_len($result, $i) { 00340 $info = mysqli_fetch_field_direct($result, $i); 00341 // stdClass::$length will be integrated in 00342 // mysqli-ext when mysql4.1 has been released. 00343 return @$info->length; 00344 } 00345 00346 function PMA_DBI_field_name($result, $i) { 00347 $info = mysqli_fetch_field_direct($result, $i); 00348 return $info->name; 00349 } 00350 00351 function PMA_DBI_field_flags($result, $i) { 00352 $f = mysqli_fetch_field_direct($result, $i); 00353 $f = $f->flags; 00354 $flags = ''; 00355 if ($f & UNIQUE_FLAG) { $flags .= 'unique ';} 00356 if ($f & NUM_FLAG) { $flags .= 'num ';} 00357 if ($f & PART_KEY_FLAG) { $flags .= 'part_key ';} 00358 if ($f & SET_FLAG) { $flags .= 'set ';} 00359 if ($f & TIMESTAMP_FLAG) { $flags .= 'timestamp ';} 00360 if ($f & AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';} 00361 if ($f & ENUM_FLAG) { $flags .= 'enum ';} 00362 if ($f & BINARY_FLAG) { $flags .= 'binary ';} 00363 if ($f & ZEROFILL_FLAG) { $flags .= 'zerofill ';} 00364 if ($f & UNSIGNED_FLAG) { $flags .= 'unsigned ';} 00365 if ($f & BLOB_FLAG) { $flags .= 'blob ';} 00366 if ($f & MULTIPLE_KEY_FLAG) { $flags .= 'multiple_key ';} 00367 if ($f & UNIQUE_KEY_FLAG) { $flags .= 'unique_key ';} 00368 if ($f & PRI_KEY_FLAG) { $flags .= 'primary_key ';} 00369 if ($f & NOT_NULL_FLAG) { $flags .= 'not_null ';} 00370 return PMA_convert_display_charset(trim($flags)); 00371 } 00372 00373 ?>