typo3/ext/phpmyadmin/modsub/phpmyadmin-2.6.2/libraries/dbi/mysqli.dbi.lib.php

Go to the documentation of this file.
00001 <?php
00002 /* $Id: mysqli.dbi.lib.php,v 2.36 2005/03/24 20:57:00 rabus 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         $fields = mysqli_fetch_fields($result);
00145         $i = 0;
00146         for ($i = 0; $i < $num; $i++) {
00147             if (!$meta) {
00148                 /* No meta information available -> we guess that it should be converted */
00149                 if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
00150                 if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
00151             } else {
00152                 /* Meta information available -> check type of field and convert it according to the type */
00153                 if (stristr($fields[$i]->type, 'BLOB') || stristr($fields[$i]->type, 'BINARY')) {
00154                     if (isset($data[$i])) $ret[$i] = $data[$i];
00155                     if (isset($data[$fields[$i]->name])) $ret[PMA_convert_display_charset($fields[$i]->name)] = $data[$fields[$i]->name];
00156                 } else {
00157                     if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
00158                     if (isset($data[$fields[$i]->name])) $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]);
00159                 }
00160             }
00161         }
00162         return $ret;
00163     }
00164 }
00165 
00166 function PMA_DBI_fetch_array($result) {
00167     return PMA_mysqli_fetch_array($result, MYSQLI_BOTH);
00168 }
00169 
00170 function PMA_DBI_fetch_assoc($result) {
00171     return PMA_mysqli_fetch_array($result, MYSQLI_ASSOC);
00172 }
00173 
00174 function PMA_DBI_fetch_row($result) {
00175     return PMA_mysqli_fetch_array($result, MYSQLI_NUM);
00176 }
00177 
00178 function PMA_DBI_free_result($result) {
00179     if (!is_bool($result)) {
00180         return mysqli_free_result($result);
00181     } else {
00182         return 0;
00183     }
00184 }
00185 
00186 function PMA_DBI_getError($link = NULL) {
00187     unset($GLOBALS['errno']);
00188     if (empty($link)) {
00189         if (isset($GLOBALS['userlink'])) {
00190             $link = $GLOBALS['userlink'];
00191             // Do not stop now. We still can get the error code
00192             // with mysqli_connect_errno()
00193 //        } else {
00194 //            return FALSE;
00195         }
00196     }
00197 
00198     if (mysqli_connect_errno()) {
00199         $error = mysqli_connect_errno();
00200         $error_message = mysqli_connect_error();
00201     } elseif ( !empty($link) && mysqli_errno($link)) {
00202         $error = mysqli_errno($link);
00203         $error_message = mysqli_error($link);
00204     } 
00205 
00206     // keep the error number for further check after the call to PMA_DBI_getError()
00207     if (!empty($error)) {
00208         $GLOBALS['errno'] = $error;
00209     } else {
00210         return FALSE;
00211     }
00212 
00213 
00214     if ($error && $error == 2002) {
00215         $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
00216     } elseif ($error && defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) {
00217         $error = '#' . ((string) $error) . ' - ' . $error_message;
00218     } elseif ($error) {
00219         $error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset($error_message);
00220     }
00221     return $error;
00222 }
00223 
00224 function PMA_DBI_close($link = NULL) {
00225     if (empty($link)) {
00226         if (isset($GLOBALS['userlink'])) {
00227             $link = $GLOBALS['userlink'];
00228         } else {
00229             return FALSE;
00230         }
00231     }
00232     return @mysqli_close($link);
00233 }
00234 
00235 function PMA_DBI_num_rows($result) {
00236     // see the note for PMA_DBI_try_query();
00237     if (!is_bool($result)) {
00238         return @mysqli_num_rows($result);
00239     } else {
00240         return 0;
00241     }
00242 }
00243 
00244 function PMA_DBI_insert_id($link = '') {
00245     if (empty($link)) {
00246         if (isset($GLOBALS['userlink'])) {
00247             $link = $GLOBALS['userlink'];
00248         } else {
00249             return FALSE;
00250         }
00251     }
00252     return mysqli_insert_id($link);
00253 }
00254 
00255 function PMA_DBI_affected_rows($link = NULL) {
00256     if (empty($link)) {
00257         if (isset($GLOBALS['userlink'])) {
00258             $link = $GLOBALS['userlink'];
00259         } else {
00260             return FALSE;
00261         }
00262     }
00263     return mysqli_affected_rows($link);
00264 }
00265 
00266 function PMA_DBI_get_fields_meta($result) {
00267     // Build an associative array for a type look up
00268     $typeAr = Array();
00269     $typeAr[MYSQLI_TYPE_DECIMAL]     = 'real';
00270     $typeAr[MYSQLI_TYPE_TINY]        = 'int';
00271     $typeAr[MYSQLI_TYPE_SHORT]       = 'int';
00272     $typeAr[MYSQLI_TYPE_LONG]        = 'int';
00273     $typeAr[MYSQLI_TYPE_FLOAT]       = 'real';
00274     $typeAr[MYSQLI_TYPE_DOUBLE]      = 'real';
00275     $typeAr[MYSQLI_TYPE_NULL]        = 'null';
00276     $typeAr[MYSQLI_TYPE_TIMESTAMP]   = 'timestamp';
00277     $typeAr[MYSQLI_TYPE_LONGLONG]    = 'int';
00278     $typeAr[MYSQLI_TYPE_INT24]       = 'int';
00279     $typeAr[MYSQLI_TYPE_DATE]        = 'date';
00280     $typeAr[MYSQLI_TYPE_TIME]        = 'time';
00281     $typeAr[MYSQLI_TYPE_DATETIME]    = 'datetime';
00282     $typeAr[MYSQLI_TYPE_YEAR]        = 'year';
00283     $typeAr[MYSQLI_TYPE_NEWDATE]     = 'date';
00284     $typeAr[MYSQLI_TYPE_ENUM]        = 'unknown';
00285     $typeAr[MYSQLI_TYPE_SET]         = 'unknown';
00286     $typeAr[MYSQLI_TYPE_TINY_BLOB]   = 'blob';
00287     $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
00288     $typeAr[MYSQLI_TYPE_LONG_BLOB]   = 'blob';
00289     $typeAr[MYSQLI_TYPE_BLOB]        = 'blob';
00290     $typeAr[MYSQLI_TYPE_VAR_STRING]  = 'string';
00291     $typeAr[MYSQLI_TYPE_STRING]      = 'string';
00292     $typeAr[MYSQLI_TYPE_CHAR]        = 'string';
00293     $typeAr[MYSQLI_TYPE_GEOMETRY]    = 'unknown';
00294 
00295     $fields = mysqli_fetch_fields($result);
00296     foreach ($fields as $k => $field) {
00297         $fields[$k]->type = $typeAr[$fields[$k]->type];
00298         $fields[$k]->flags = PMA_DBI_field_flags($result, $k);
00299         
00300         // Enhance the field objects for mysql-extension compatibilty
00301         $flags = explode(' ', $fields[$k]->flags);
00302         array_unshift($flags, 'dummy');
00303         $fields[$k]->multiple_key = (int)(array_search('multiple_key', $flags, true) > 0);
00304         $fields[$k]->primary_key  = (int)(array_search('primary_key', $flags, true) > 0);
00305         $fields[$k]->unique_key   = (int)(array_search('unique_key', $flags, true) > 0);
00306         $fields[$k]->not_null     = (int)(array_search('not_null', $flags, true) > 0);
00307         $fields[$k]->unsigned     = (int)(array_search('unsigned', $flags, true) > 0);
00308         $fields[$k]->zerofill     = (int)(array_search('zerofill', $flags, true) > 0);
00309         $fields[$k]->numeric      = (int)(array_search('num', $flags, true) > 0);
00310         $fields[$k]->blob         = (int)(array_search('blob', $flags, true) > 0);
00311     }
00312     return $fields;
00313 }
00314 
00315 function PMA_DBI_num_fields($result) {
00316     return mysqli_num_fields($result);
00317 }
00318 
00319 function PMA_DBI_field_len($result, $i) {
00320     $info = mysqli_fetch_field_direct($result, $i);
00321     // stdClass::$length will be integrated in 
00322     // mysqli-ext when mysql4.1 has been released.
00323     return @$info->length;
00324 }
00325 
00326 function PMA_DBI_field_name($result, $i) {
00327     $info = mysqli_fetch_field_direct($result, $i);
00328     return $info->name;
00329 }
00330 
00331 function PMA_DBI_field_flags($result, $i) {
00332     $f = mysqli_fetch_field_direct($result, $i);
00333     $f = $f->flags;
00334     $flags = '';
00335     if ($f & UNIQUE_FLAG)         { $flags .= 'unique ';}
00336     if ($f & NUM_FLAG)            { $flags .= 'num ';}
00337     if ($f & PART_KEY_FLAG)       { $flags .= 'part_key ';}
00338     if ($f & SET_FLAG)            { $flags .= 'set ';}
00339     if ($f & TIMESTAMP_FLAG)      { $flags .= 'timestamp ';}
00340     if ($f & AUTO_INCREMENT_FLAG) { $flags .= 'auto_increment ';}
00341     if ($f & ENUM_FLAG)           { $flags .= 'enum ';}
00342     if ($f & BINARY_FLAG)         { $flags .= 'binary ';}
00343     if ($f & ZEROFILL_FLAG)       { $flags .= 'zerofill ';}
00344     if ($f & UNSIGNED_FLAG)       { $flags .= 'unsigned ';}
00345     if ($f & BLOB_FLAG)           { $flags .= 'blob ';}
00346     if ($f & MULTIPLE_KEY_FLAG)   { $flags .= 'multiple_key ';}
00347     if ($f & UNIQUE_KEY_FLAG)     { $flags .= 'unique_key ';}
00348     if ($f & PRI_KEY_FLAG)        { $flags .= 'primary_key ';}
00349     if ($f & NOT_NULL_FLAG)       { $flags .= 'not_null ';}
00350     return PMA_convert_display_charset(trim($flags));
00351 }
00352 
00353 ?>

Generated on Wed Aug 10 07:55:58 2005 for TYPO3 3.8.0 by  doxygen 1.4.3-20050530