Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / dbi / mysql.dbi.lib.php
blobc5ff26f174ea7cbf51f01b018b4a603fcebb48a3
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)
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 ($GLOBALS['cfg']['DBG']['sql']) {
179 $time = microtime(true) - $time;
181 $hash = md5($query);
183 if (isset($_SESSION['debug']['queries'][$hash])) {
184 $_SESSION['debug']['queries'][$hash]['count']++;
185 } else {
186 $_SESSION['debug']['queries'][$hash] = array();
187 $_SESSION['debug']['queries'][$hash]['count'] = 1;
188 $_SESSION['debug']['queries'][$hash]['query'] = $query;
189 $_SESSION['debug']['queries'][$hash]['time'] = $time;
192 $trace = array();
193 foreach (debug_backtrace() as $trace_step) {
194 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
195 . $trace_step['line'] . ': '
196 . (isset($trace_step['class']) ? $trace_step['class'] : '')
197 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
198 . (isset($trace_step['type']) ? $trace_step['type'] : '')
199 . (isset($trace_step['function']) ? $trace_step['function'] : '')
200 . '('
201 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
202 . ')'
205 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
207 if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
208 PMA_Tracker::handleQuery($query);
211 return $r;
214 function PMA_DBI_fetch_array($result)
216 return mysql_fetch_array($result, MYSQL_BOTH);
219 function PMA_DBI_fetch_assoc($result) {
220 return mysql_fetch_array($result, MYSQL_ASSOC);
223 function PMA_DBI_fetch_row($result)
225 return mysql_fetch_array($result, MYSQL_NUM);
229 * Adjusts the result pointer to an arbitrary row in the result
231 * @uses mysql_data_seek()
232 * @param $result
233 * @param $offset
234 * @return boolean true on success, false on failure
236 function PMA_DBI_data_seek($result, $offset)
238 return mysql_data_seek($result, $offset);
242 * Frees the memory associated with the results
244 * @param result $result,... one or more mysql result resources
246 function PMA_DBI_free_result()
248 foreach (func_get_args() as $result) {
249 if (is_resource($result)
250 && get_resource_type($result) === 'mysql result') {
251 mysql_free_result($result);
257 * Returns a string representing the type of connection used
258 * @uses mysql_get_host_info()
259 * @uses $GLOBALS['userlink'] as default for $link
260 * @param resource $link mysql link
261 * @return string type of connection used
263 function PMA_DBI_get_host_info($link = null)
265 if (null === $link) {
266 if (isset($GLOBALS['userlink'])) {
267 $link = $GLOBALS['userlink'];
268 } else {
269 return false;
272 return mysql_get_host_info($link);
276 * Returns the version of the MySQL protocol used
277 * @uses mysql_get_proto_info()
278 * @uses $GLOBALS['userlink'] as default for $link
279 * @param resource $link mysql link
280 * @return integer version of the MySQL protocol used
282 function PMA_DBI_get_proto_info($link = null)
284 if (null === $link) {
285 if (isset($GLOBALS['userlink'])) {
286 $link = $GLOBALS['userlink'];
287 } else {
288 return false;
291 return mysql_get_proto_info($link);
295 * returns a string that represents the client library version
296 * @uses mysql_get_client_info()
297 * @return string MySQL client library version
299 function PMA_DBI_get_client_info()
301 return mysql_get_client_info();
305 * returns last error message or false if no errors occured
307 * @uses PMA_DBI_convert_message()
308 * @uses $GLOBALS['errno']
309 * @uses $GLOBALS['userlink']
310 * @uses mysql_errno()
311 * @uses mysql_error()
312 * @uses defined()
313 * @uses PMA_generate_common_url()
314 * @param resource $link mysql link
315 * @return string|boolean $error or false
317 function PMA_DBI_getError($link = null)
319 $GLOBALS['errno'] = 0;
321 /* Treat false same as null because of controllink */
322 if ($link === false) {
323 $link = null;
326 if (null === $link && isset($GLOBALS['userlink'])) {
327 $link =& $GLOBALS['userlink'];
329 // Do not stop now. On the initial connection, we don't have a $link,
330 // we don't have a $GLOBALS['userlink'], but we can catch the error code
331 // } else {
332 // return false;
335 if (null !== $link && false !== $link) {
336 $error_number = mysql_errno($link);
337 $error_message = mysql_error($link);
338 } else {
339 $error_number = mysql_errno();
340 $error_message = mysql_error();
342 if (0 == $error_number) {
343 return false;
346 // keep the error number for further check after the call to PMA_DBI_getError()
347 $GLOBALS['errno'] = $error_number;
349 if (! empty($error_message)) {
350 $error_message = PMA_DBI_convert_message($error_message);
353 $error_message = htmlspecialchars($error_message);
355 // Some errors messages cannot be obtained by mysql_error()
356 if ($error_number == 2002) {
357 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
358 } elseif ($error_number == 2003) {
359 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding');
360 } elseif ($error_number == 1005) {
361 /* InnoDB contraints, see
362 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
364 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
365 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
366 '">' . __('Details...') . '</a>)';
367 } else {
368 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
370 return $error;
373 function PMA_DBI_num_rows($result)
375 if (!is_bool($result)) {
376 return mysql_num_rows($result);
377 } else {
378 return 0;
382 function PMA_DBI_insert_id($link = null)
384 if (empty($link)) {
385 if (isset($GLOBALS['userlink'])) {
386 $link = $GLOBALS['userlink'];
387 } else {
388 return false;
391 //$insert_id = mysql_insert_id($link);
392 // if the primary key is BIGINT we get an incorrect result
393 // (sometimes negative, sometimes positive)
394 // and in the present function we don't know if the PK is BIGINT
395 // so better play safe and use LAST_INSERT_ID()
397 // by the way, no problem with mysqli_insert_id()
398 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
401 function PMA_DBI_affected_rows($link = null)
403 if (empty($link)) {
404 if (isset($GLOBALS['userlink'])) {
405 $link = $GLOBALS['userlink'];
406 } else {
407 return false;
410 return mysql_affected_rows($link);
414 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
416 function PMA_DBI_get_fields_meta($result)
418 $fields = array();
419 $num_fields = mysql_num_fields($result);
420 for ($i = 0; $i < $num_fields; $i++) {
421 $fields[] = mysql_fetch_field($result, $i);
423 return $fields;
426 function PMA_DBI_num_fields($result)
428 return mysql_num_fields($result);
431 function PMA_DBI_field_len($result, $i)
433 return mysql_field_len($result, $i);
436 function PMA_DBI_field_name($result, $i)
438 return mysql_field_name($result, $i);
441 function PMA_DBI_field_flags($result, $i)
443 return mysql_field_flags($result, $i);