home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / dbi / mysql.dbi.lib.php next >
Encoding:
PHP Script  |  2004-09-28  |  8.1 KB  |  268 lines

  1. <?php
  2. /* $Id: mysql.dbi.lib.php,v 2.33 2004/09/28 10:41:49 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Interface to the classic MySQL extension
  7.  */
  8.  
  9. /**
  10.  * Loads the mysql extensions if it is not loaded yet
  11.  */
  12. if (!@function_exists('mysql_connect')) {
  13.     PMA_dl('mysql');
  14. }
  15.  
  16. // check whether mysql is available
  17. if (!@function_exists('mysql_connect')) {
  18.     require_once('./libraries/header_http.inc.php');
  19.     echo sprintf($strCantLoad, 'mysql') . '<br />' . "\n"
  20.          . '<a href="./Documentation.html#faqmysql" target="documentation">' . $GLOBALS['strDocu'] . '</a>' . "\n";
  21.     exit;
  22. }
  23.  
  24. // MySQL client API
  25. if (!defined('PMA_MYSQL_CLIENT_API')) {
  26.     if (function_exists('mysql_get_client_info')) {
  27.         $client_api = explode('.', mysql_get_client_info());
  28.         define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
  29.         unset($client_api);
  30.     } else {
  31.         define('PMA_MYSQL_CLIENT_API', 32332); // always expect the worst...
  32.     }
  33. }
  34.  
  35. function PMA_DBI_connect($user, $password) {
  36.     global $cfg, $php_errormsg;
  37.  
  38.     $server_port   = (empty($cfg['Server']['port']))
  39.                    ? ''
  40.                    : ':' . $cfg['Server']['port'];
  41.  
  42.     if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  43.         $cfg['Server']['socket'] = '';
  44.     }
  45.  
  46.     $server_socket = (empty($cfg['Server']['socket']))
  47.                    ? ''
  48.                    : ':' . $cfg['Server']['socket'];
  49.  
  50.     if (PMA_PHP_INT_VERSION >= 40300 && PMA_MYSQL_CLIENT_API >= 32349) {
  51.         $client_flags = $cfg['Server']['compress'] && defined('MYSQL_CLIENT_COMPRESS') ? MYSQL_CLIENT_COMPRESS : 0;
  52.         // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
  53.         // for the case where the client library was not compiled
  54.         // with --enable-local-infile
  55.         $client_flags |= 128;
  56.     }
  57.  
  58.     if (empty($client_flags)) {
  59.         $connect_func = 'mysql_' . ($cfg['PersistentConnections'] ? 'p' : '') . 'connect';
  60.         $link = @$connect_func($cfg['Server']['host'] . $server_port . $server_socket, $user, $password);
  61.     } else {
  62.         if ($cfg['PersistentConnections']) {
  63.             $link = @mysql_pconnect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, $client_flags);
  64.         } else {
  65.             $link = @mysql_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, FALSE, $client_flags);
  66.         }
  67.     }
  68.  
  69.     if (empty($link)) {
  70.         PMA_auth_fails();
  71.     } // end if
  72.     
  73.     PMA_DBI_postConnect($link);
  74.  
  75.     return $link;
  76. }
  77.  
  78. function PMA_DBI_select_db($dbname, $link = NULL) {
  79.     if (empty($link)) {
  80.         if (isset($GLOBALS['userlink'])) {
  81.             $link = $GLOBALS['userlink'];
  82.         } else {
  83.             return FALSE;
  84.         }
  85.     }
  86.     if (PMA_MYSQL_INT_VERSION < 40100) {
  87.         $dbname = PMA_convert_charset($dbname);
  88.     }
  89.     return mysql_select_db($dbname, $link);
  90. }
  91.  
  92. function PMA_DBI_try_query($query, $link = NULL, $options = 0) {
  93.     if (empty($link)) {
  94.         if (isset($GLOBALS['userlink'])) {
  95.             $link = $GLOBALS['userlink'];
  96.         } else {
  97.             return FALSE;
  98.         }
  99.     }
  100.     if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) {
  101.         $query = PMA_convert_charset($query);
  102.     }
  103.     if ($options == ($options | PMA_DBI_QUERY_STORE)) {
  104.         return @mysql_query($query, $link);
  105.     } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
  106.         return @mysql_unbuffered_query($query, $link);
  107.     } else {
  108.         return @mysql_query($query, $link);
  109.     }
  110. }
  111.  
  112. // The following function is meant for internal use only.
  113. // Do not call it from outside this library!
  114. function PMA_mysql_fetch_array($result, $type = FALSE) {
  115.     global $cfg, $allow_recoding, $charset, $convcharset;
  116.  
  117.     if ($type != FALSE) {
  118.         $data = mysql_fetch_array($result, $type);
  119.     } else {
  120.         $data = mysql_fetch_array($result);
  121.     }
  122.  
  123.     /* No data returned => do not touch it */
  124.     if (! $data) return $data;
  125.     
  126.     if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100
  127.         || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
  128.         /* No recoding -> return data as we got them */
  129.         return $data;
  130.     } else {
  131.         $ret = array();
  132.         $num = mysql_num_fields($result);
  133.         $i = 0;
  134.         for ($i = 0; $i < $num; $i++) {
  135.             $name = mysql_field_name($result, $i);
  136.             $flags = mysql_field_flags($result, $i);
  137.             /* Field is BINARY (either marked manually, or it is BLOB) => do not convert it */
  138.             if (stristr($flags, 'BINARY')) {
  139.                 if (isset($data[$i])) $ret[$i] = $data[$i];
  140.                 if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = $data[$name];
  141.             } else {
  142.                 if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
  143.                 if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
  144.             }
  145.         }
  146.         return $ret;
  147.     }
  148. }
  149.  
  150. function PMA_DBI_fetch_array($result) {
  151.     return PMA_mysql_fetch_array($result);
  152. }
  153.  
  154. function PMA_DBI_fetch_assoc($result) {
  155.     return PMA_mysql_fetch_array($result, MYSQL_ASSOC);
  156. }
  157.  
  158. function PMA_DBI_fetch_row($result) {
  159.     return PMA_mysql_fetch_array($result, MYSQL_NUM);
  160. }
  161.  
  162. function PMA_DBI_free_result($result) {
  163.     return @mysql_free_result($result);
  164. }
  165.  
  166. function PMA_DBI_getError($link = NULL) {
  167.     unset($GLOBALS['errno']); 
  168.     if (empty($link)) {
  169.         if (isset($GLOBALS['userlink'])) {
  170.             $link = $GLOBALS['userlink'];
  171.  
  172. // Do not stop now. On the initial connection, we don't have a $link,
  173. // we don't have a $GLOBALS['userlink'], but we can catch the error code
  174. //        } else {
  175. //            return FALSE;
  176.        }
  177.     }
  178.  
  179.     if (mysql_errno()) {
  180.         $error = mysql_errno();
  181.         $error_message = mysql_error();
  182.     } elseif ($link) {
  183.         $error = mysql_errno($link);
  184.         $error_message = mysql_error($link);
  185.     }
  186.  
  187.     // keep the error number for further check after the call to PMA_DBI_getError() 
  188.     if (isset($error) && $error) {
  189.         $GLOBALS['errno'] = $error;
  190.     } else {
  191.         return FALSE;
  192.     }
  193.  
  194. // Some errors messages cannot be obtained by mysql_error()
  195.     if ($error && $error == 2003) {
  196.         $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding'];
  197.     } elseif ($error && defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) {
  198.         $error = '#' . ((string) $error) . ' - ' . $error_message;
  199.     } elseif ($error) {
  200.         $error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset($error_message);
  201.     }
  202.     return $error;
  203. }
  204.  
  205. function PMA_DBI_close($link = NULL) {
  206.     if (empty($link)) {
  207.         if (isset($GLOBALS['userlink'])) {
  208.             $link = $GLOBALS['userlink'];
  209.         } else {
  210.             return FALSE;
  211.         }
  212.     }
  213.     return @mysql_close($link);
  214. }
  215.  
  216. function PMA_DBI_num_rows($result) {
  217.     return mysql_num_rows($result);
  218. }
  219.  
  220. function PMA_DBI_insert_id($link = NULL) {
  221.     if (empty($link)) {
  222.         if (isset($GLOBALS['userlink'])) {
  223.             $link = $GLOBALS['userlink'];
  224.         } else {
  225.             return FALSE;
  226.         }
  227.     }
  228.     return mysql_insert_id($link);
  229. }
  230.  
  231. function PMA_DBI_affected_rows($link = NULL) {
  232.     if (empty($link)) {
  233.         if (isset($GLOBALS['userlink'])) {
  234.             $link = $GLOBALS['userlink'];
  235.         } else {
  236.             return FALSE;
  237.         }
  238.     }
  239.     return mysql_affected_rows($link);
  240. }
  241.  
  242. function PMA_DBI_get_fields_meta($result) {
  243.     $fields       = array();
  244.     $num_fields   = mysql_num_fields($result);
  245.     for ($i = 0; $i < $num_fields; $i++) {
  246.         $fields[] = PMA_convert_display_charset(mysql_fetch_field($result, $i));
  247.     }
  248.     return $fields;
  249. }
  250.  
  251. function PMA_DBI_num_fields($result) {
  252.     return mysql_num_fields($result);
  253. }
  254.  
  255. function PMA_DBI_field_len($result, $i) {
  256.     return mysql_field_len($result, $i);
  257. }
  258.  
  259. function PMA_DBI_field_name($result, $i) {
  260.     return mysql_field_name($result, $i);
  261. }
  262.  
  263. function PMA_DBI_field_flags($result, $i) {
  264.     return PMA_convert_display_charset(mysql_field_flags($result, $i));
  265. }
  266.  
  267. ?>
  268.