Translation update done using Pootle.
[phpmyadmin-themes.git] / libraries / dbi / mysql.dbi.lib.php
blobc4f22fb28aeceb8af499eb5a9bdeb907be7c8792
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface to the classic MySQL extension
6 * @package phpMyAdmin-DBI-MySQL
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once './libraries/logging.lib.php';
14 /**
15 * MySQL client API
17 if (! defined('PMA_MYSQL_CLIENT_API')) {
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);
23 function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistant=false)
25 global $cfg;
27 if (empty($client_flags)) {
28 if ($cfg['PersistentConnections'] || $persistant) {
29 $link = @mysql_pconnect($server, $user, $password);
30 } else {
31 $link = @mysql_connect($server, $user, $password);
33 } else {
34 if ($cfg['PersistentConnections'] || $persistant) {
35 $link = @mysql_pconnect($server, $user, $password, $client_flags);
36 } else {
37 $link = @mysql_connect($server, $user, $password, false, $client_flags);
41 return $link;
43 /**
44 * @param string $user mysql user name
45 * @param string $password mysql user password
46 * @param boolean $is_controluser
47 * @param array $server host/port/socket/persistant
48 * @param boolean $auxiliary_connection (when true, don't go back to login if connection fails)
49 * @return mixed false on error or a mysqli object on success
51 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
53 global $cfg, $php_errormsg;
55 if ($server) {
56 $server_port = (empty($server['port']))
57 ? ''
58 : ':' . (int)$server['port'];
59 $server_socket = (empty($server['socket']))
60 ? ''
61 : ':' . $server['socket'];
62 $server_persistant = (empty($server['persistant']))
63 ? false
64 : true;
65 } else {
66 $server_port = (empty($cfg['Server']['port']))
67 ? ''
68 : ':' . (int)$cfg['Server']['port'];
69 $server_socket = (empty($cfg['Server']['socket']))
70 ? ''
71 : ':' . $cfg['Server']['socket'];
74 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
75 $cfg['Server']['socket'] = '';
78 $client_flags = 0;
80 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
81 // for the case where the client library was not compiled
82 // with --enable-local-infile
83 $client_flags |= 128;
85 /* Optionally compress connection */
86 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
87 $client_flags |= MYSQL_CLIENT_COMPRESS;
90 /* Optionally enable SSL */
91 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
92 $client_flags |= MYSQL_CLIENT_SSL;
95 if (!$server) {
96 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? NULL : $client_flags);
98 // Retry with empty password if we're allowed to
99 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
100 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? NULL : $client_flags);
102 } else {
103 if (!isset($server['host'])) {
104 $link = PMA_DBI_real_connect($server_socket, $user, $password, NULL, $server_persistant);
105 } else {
106 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, NULL, $server_persistant);
109 if (empty($link)) {
110 if ($is_controluser) {
111 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING);
112 return false;
114 // we could be calling PMA_DBI_connect() to connect to another
115 // server, for example in the Synchronize feature, so do not
116 // go back to main login if it fails
117 if (! $auxiliary_connection) {
118 PMA_log_user($user, 'mysql-denied');
119 PMA_auth_fails();
120 } else {
121 return false;
123 } // end if
124 if (! $server) {
125 PMA_DBI_postConnect($link, $is_controluser);
127 return $link;
131 * select a db
133 * @param string $dbname name of db to select
134 * @param resource $link mysql link resource
135 * @return boolean success
137 function PMA_DBI_select_db($dbname, $link = null)
139 if (empty($link)) {
140 if (isset($GLOBALS['userlink'])) {
141 $link = $GLOBALS['userlink'];
142 } else {
143 return false;
146 return mysql_select_db($dbname, $link);
150 * runs a query and returns the result
152 * @param string $query query to run
153 * @param resource $link mysql link resource
154 * @param integer $options
155 * @return mixed
157 function PMA_DBI_try_query($query, $link = null, $options = 0, $cache_affected_rows = true)
159 if (empty($link)) {
160 if (isset($GLOBALS['userlink'])) {
161 $link = $GLOBALS['userlink'];
162 } else {
163 return false;
167 if ($GLOBALS['cfg']['DBG']['sql']) {
168 $time = microtime(true);
170 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
171 $r = mysql_query($query, $link);
172 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
173 $r = mysql_unbuffered_query($query, $link);
174 } else {
175 $r = mysql_query($query, $link);
178 if ($cache_affected_rows) {
179 $GLOBALS['cached_affected_rows'] = PMA_DBI_affected_rows($link, $get_from_cache = false);
182 if ($GLOBALS['cfg']['DBG']['sql']) {
183 $time = microtime(true) - $time;
185 $hash = md5($query);
187 if (isset($_SESSION['debug']['queries'][$hash])) {
188 $_SESSION['debug']['queries'][$hash]['count']++;
189 } else {
190 $_SESSION['debug']['queries'][$hash] = array();
191 $_SESSION['debug']['queries'][$hash]['count'] = 1;
192 $_SESSION['debug']['queries'][$hash]['query'] = $query;
193 $_SESSION['debug']['queries'][$hash]['time'] = $time;
196 $trace = array();
197 foreach (debug_backtrace() as $trace_step) {
198 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
199 . $trace_step['line'] . ': '
200 . (isset($trace_step['class']) ? $trace_step['class'] : '')
201 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
202 . (isset($trace_step['type']) ? $trace_step['type'] : '')
203 . (isset($trace_step['function']) ? $trace_step['function'] : '')
204 . '('
205 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
206 . ')'
209 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
211 if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
212 PMA_Tracker::handleQuery($query);
215 return $r;
218 function PMA_DBI_fetch_array($result)
220 return mysql_fetch_array($result, MYSQL_BOTH);
223 function PMA_DBI_fetch_assoc($result) {
224 return mysql_fetch_array($result, MYSQL_ASSOC);
227 function PMA_DBI_fetch_row($result)
229 return mysql_fetch_array($result, MYSQL_NUM);
233 * Adjusts the result pointer to an arbitrary row in the result
235 * @uses mysql_data_seek()
236 * @param $result
237 * @param $offset
238 * @return boolean true on success, false on failure
240 function PMA_DBI_data_seek($result, $offset)
242 return mysql_data_seek($result, $offset);
246 * Frees the memory associated with the results
248 * @param result $result,... one or more mysql result resources
250 function PMA_DBI_free_result()
252 foreach (func_get_args() as $result) {
253 if (is_resource($result)
254 && get_resource_type($result) === 'mysql result') {
255 mysql_free_result($result);
261 * Returns a string representing the type of connection used
262 * @uses mysql_get_host_info()
263 * @uses $GLOBALS['userlink'] as default for $link
264 * @param resource $link mysql link
265 * @return string type of connection used
267 function PMA_DBI_get_host_info($link = null)
269 if (null === $link) {
270 if (isset($GLOBALS['userlink'])) {
271 $link = $GLOBALS['userlink'];
272 } else {
273 return false;
276 return mysql_get_host_info($link);
280 * Returns the version of the MySQL protocol used
281 * @uses mysql_get_proto_info()
282 * @uses $GLOBALS['userlink'] as default for $link
283 * @param resource $link mysql link
284 * @return integer version of the MySQL protocol used
286 function PMA_DBI_get_proto_info($link = null)
288 if (null === $link) {
289 if (isset($GLOBALS['userlink'])) {
290 $link = $GLOBALS['userlink'];
291 } else {
292 return false;
295 return mysql_get_proto_info($link);
299 * returns a string that represents the client library version
300 * @uses mysql_get_client_info()
301 * @return string MySQL client library version
303 function PMA_DBI_get_client_info()
305 return mysql_get_client_info();
309 * returns last error message or false if no errors occured
311 * @uses PMA_DBI_convert_message()
312 * @uses $GLOBALS['errno']
313 * @uses $GLOBALS['userlink']
314 * @uses mysql_errno()
315 * @uses mysql_error()
316 * @uses defined()
317 * @uses PMA_generate_common_url()
318 * @param resource $link mysql link
319 * @return string|boolean $error or false
321 function PMA_DBI_getError($link = null)
323 $GLOBALS['errno'] = 0;
325 /* Treat false same as null because of controllink */
326 if ($link === false) {
327 $link = null;
330 if (null === $link && isset($GLOBALS['userlink'])) {
331 $link =& $GLOBALS['userlink'];
333 // Do not stop now. On the initial connection, we don't have a $link,
334 // we don't have a $GLOBALS['userlink'], but we can catch the error code
335 // } else {
336 // return false;
339 if (null !== $link && false !== $link) {
340 $error_number = mysql_errno($link);
341 $error_message = mysql_error($link);
342 } else {
343 $error_number = mysql_errno();
344 $error_message = mysql_error();
346 if (0 == $error_number) {
347 return false;
350 // keep the error number for further check after the call to PMA_DBI_getError()
351 $GLOBALS['errno'] = $error_number;
353 if (! empty($error_message)) {
354 $error_message = PMA_DBI_convert_message($error_message);
357 $error_message = htmlspecialchars($error_message);
359 // Some errors messages cannot be obtained by mysql_error()
360 if ($error_number == 2002) {
361 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
362 } elseif ($error_number == 2003) {
363 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding');
364 } elseif ($error_number == 1005) {
365 /* InnoDB contraints, see
366 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
368 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
369 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
370 '">' . __('Details...') . '</a>)';
371 } else {
372 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
374 return $error;
377 function PMA_DBI_num_rows($result)
379 if (!is_bool($result)) {
380 return mysql_num_rows($result);
381 } else {
382 return 0;
386 function PMA_DBI_insert_id($link = null)
388 if (empty($link)) {
389 if (isset($GLOBALS['userlink'])) {
390 $link = $GLOBALS['userlink'];
391 } else {
392 return false;
395 // If the primary key is BIGINT we get an incorrect result
396 // (sometimes negative, sometimes positive)
397 // and in the present function we don't know if the PK is BIGINT
398 // so better play safe and use LAST_INSERT_ID()
400 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
404 * returns the number of rows affected by last query
406 * @uses $GLOBALS['userlink']
407 * @uses mysql_affected_rows()
408 * @param object mysql $link the mysql object
409 * @param boolean $get_from_cache
410 * @return string integer
412 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
414 if (empty($link)) {
415 if (isset($GLOBALS['userlink'])) {
416 $link = $GLOBALS['userlink'];
417 } else {
418 return false;
422 if ($get_from_cache) {
423 return $GLOBALS['cached_affected_rows'];
424 } else {
425 return mysql_affected_rows($link);
430 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
432 function PMA_DBI_get_fields_meta($result)
434 $fields = array();
435 $num_fields = mysql_num_fields($result);
436 for ($i = 0; $i < $num_fields; $i++) {
437 $fields[] = mysql_fetch_field($result, $i);
439 return $fields;
442 function PMA_DBI_num_fields($result)
444 return mysql_num_fields($result);
447 function PMA_DBI_field_len($result, $i)
449 return mysql_field_len($result, $i);
452 function PMA_DBI_field_name($result, $i)
454 return mysql_field_name($result, $i);
457 function PMA_DBI_field_flags($result, $i)
459 return mysql_field_flags($result, $i);