home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / dbi / mysql.dbi.lib.php next >
Encoding:
PHP Script  |  2008-06-23  |  11.5 KB  |  396 lines

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