Translated using Weblate.
[phpmyadmin.git] / libraries / dbi / mysql.dbi.lib.php
blob6e6b7e27e53348a1163f46f857bbd74501f82220
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 /**
24 * Helper function for connecting to the database server
26 * @param string $server
27 * @param string $user
28 * @param string $password
29 * @param int $client_flags
30 * @param bool $persistent
31 * @return mixed false on error or a mysql connection resource on success
33 function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistent = false)
35 global $cfg;
37 if (empty($client_flags)) {
38 if ($cfg['PersistentConnections'] || $persistent) {
39 $link = @mysql_pconnect($server, $user, $password);
40 } else {
41 $link = @mysql_connect($server, $user, $password);
43 } else {
44 if ($cfg['PersistentConnections'] || $persistent) {
45 $link = @mysql_pconnect($server, $user, $password, $client_flags);
46 } else {
47 $link = @mysql_connect($server, $user, $password, false, $client_flags);
51 return $link;
54 /**
55 * connects to the database server
57 * @param string $user mysql user name
58 * @param string $password mysql user password
59 * @param bool $is_controluser
60 * @param array $server host/port/socket/persistent
61 * @param bool $auxiliary_connection (when true, don't go back to login if connection fails)
62 * @return mixed false on error or a mysqli object on success
64 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
66 global $cfg;
68 if ($server) {
69 $server_port = (empty($server['port']))
70 ? ''
71 : ':' . (int)$server['port'];
72 $server_socket = (empty($server['socket']))
73 ? ''
74 : ':' . $server['socket'];
75 } else {
76 $server_port = (empty($cfg['Server']['port']))
77 ? ''
78 : ':' . (int)$cfg['Server']['port'];
79 $server_socket = (empty($cfg['Server']['socket']))
80 ? ''
81 : ':' . $cfg['Server']['socket'];
84 $client_flags = 0;
86 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
87 // for the case where the client library was not compiled
88 // with --enable-local-infile
89 $client_flags |= 128;
91 /* Optionally compress connection */
92 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
93 $client_flags |= MYSQL_CLIENT_COMPRESS;
96 /* Optionally enable SSL */
97 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
98 $client_flags |= MYSQL_CLIENT_SSL;
101 if (!$server) {
102 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? null : $client_flags);
104 // Retry with empty password if we're allowed to
105 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
106 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? null : $client_flags);
108 } else {
109 if (!isset($server['host'])) {
110 $link = PMA_DBI_real_connect($server_socket, $user, $password, null);
111 } else {
112 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, null);
115 if (empty($link)) {
116 if ($is_controluser) {
117 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING);
118 return false;
120 // we could be calling PMA_DBI_connect() to connect to another
121 // server, for example in the Synchronize feature, so do not
122 // go back to main login if it fails
123 if (! $auxiliary_connection) {
124 PMA_log_user($user, 'mysql-denied');
125 PMA_auth_fails();
126 } else {
127 return false;
129 } // end if
130 if (! $server) {
131 PMA_DBI_postConnect($link, $is_controluser);
133 return $link;
137 * selects given database
139 * @param string $dbname name of db to select
140 * @param resource $link mysql link resource
141 * @return bool
143 function PMA_DBI_select_db($dbname, $link = null)
145 if (empty($link)) {
146 if (isset($GLOBALS['userlink'])) {
147 $link = $GLOBALS['userlink'];
148 } else {
149 return false;
152 return mysql_select_db($dbname, $link);
156 * runs a query and returns the result
158 * @param string $query query to run
159 * @param resource $link mysql link resource
160 * @param int $options
161 * @return mixed
163 function PMA_DBI_real_query($query, $link, $options)
165 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
166 return mysql_query($query, $link);
167 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
168 return mysql_unbuffered_query($query, $link);
169 } else {
170 return mysql_query($query, $link);
175 * returns array of rows with associative and numeric keys from $result
177 * @param resource $result
178 * @return array
180 function PMA_DBI_fetch_array($result)
182 return mysql_fetch_array($result, MYSQL_BOTH);
186 * returns array of rows with associative keys from $result
188 * @param resource $result
189 * @return array
191 function PMA_DBI_fetch_assoc($result)
193 return mysql_fetch_array($result, MYSQL_ASSOC);
197 * returns array of rows with numeric keys from $result
199 * @param resource $result
200 * @return array
202 function PMA_DBI_fetch_row($result)
204 return mysql_fetch_array($result, MYSQL_NUM);
208 * Adjusts the result pointer to an arbitrary row in the result
210 * @param $result
211 * @param $offset
212 * @return bool true on success, false on failure
214 function PMA_DBI_data_seek($result, $offset)
216 return mysql_data_seek($result, $offset);
220 * Frees memory associated with the result
222 * @param resource $result
224 function PMA_DBI_free_result($result)
226 if (is_resource($result) && get_resource_type($result) === 'mysql result') {
227 mysql_free_result($result);
232 * Check if there are any more query results from a multi query
234 * @return bool false
236 function PMA_DBI_more_results()
238 // N.B.: PHP's 'mysql' extension does not support
239 // multi_queries so this function will always
240 // return false. Use the 'mysqli' extension, if
241 // you need support for multi_queries.
242 return false;
246 * Prepare next result from multi_query
248 * @return boo false
250 function PMA_DBI_next_result()
252 // N.B.: PHP's 'mysql' extension does not support
253 // multi_queries so this function will always
254 // return false. Use the 'mysqli' extension, if
255 // you need support for multi_queries.
256 return false;
260 * Returns a string representing the type of connection used
262 * @param resource $link mysql link
263 * @return string type of connection used
265 function PMA_DBI_get_host_info($link = null)
267 if (null === $link) {
268 if (isset($GLOBALS['userlink'])) {
269 $link = $GLOBALS['userlink'];
270 } else {
271 return false;
274 return mysql_get_host_info($link);
278 * Returns the version of the MySQL protocol used
280 * @param resource $link mysql link
281 * @return int version of the MySQL protocol used
283 function PMA_DBI_get_proto_info($link = null)
285 if (null === $link) {
286 if (isset($GLOBALS['userlink'])) {
287 $link = $GLOBALS['userlink'];
288 } else {
289 return false;
292 return mysql_get_proto_info($link);
296 * returns a string that represents the client library version
298 * @return string MySQL client library version
300 function PMA_DBI_get_client_info()
302 return mysql_get_client_info();
306 * returns last error message or false if no errors occured
308 * @param resource $link mysql link
309 * @return string|bool $error or false
311 function PMA_DBI_getError($link = null)
313 $GLOBALS['errno'] = 0;
315 /* Treat false same as null because of controllink */
316 if ($link === false) {
317 $link = null;
320 if (null === $link && isset($GLOBALS['userlink'])) {
321 $link =& $GLOBALS['userlink'];
323 // Do not stop now. On the initial connection, we don't have a $link,
324 // we don't have a $GLOBALS['userlink'], but we can catch the error code
325 // } else {
326 // return false;
329 if (null !== $link && false !== $link) {
330 $error_number = mysql_errno($link);
331 $error_message = mysql_error($link);
332 } else {
333 $error_number = mysql_errno();
334 $error_message = mysql_error();
336 if (0 == $error_number) {
337 return false;
340 // keep the error number for further check after the call to PMA_DBI_getError()
341 $GLOBALS['errno'] = $error_number;
343 return PMA_DBI_formatError($error_number, $error_message);
347 * returns the number of rows returned by last query
349 * @param resource $result
350 * @return string|int
352 function PMA_DBI_num_rows($result)
354 if (!is_bool($result)) {
355 return mysql_num_rows($result);
356 } else {
357 return 0;
362 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
364 * @param resource $link the mysql object
365 * @return string|int
367 function PMA_DBI_insert_id($link = null)
369 if (empty($link)) {
370 if (isset($GLOBALS['userlink'])) {
371 $link = $GLOBALS['userlink'];
372 } else {
373 return false;
376 // If the primary key is BIGINT we get an incorrect result
377 // (sometimes negative, sometimes positive)
378 // and in the present function we don't know if the PK is BIGINT
379 // so better play safe and use LAST_INSERT_ID()
381 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
385 * returns the number of rows affected by last query
387 * @param resource $link the mysql object
388 * @param bool $get_from_cache
389 * @return string|int
391 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
393 if (empty($link)) {
394 if (isset($GLOBALS['userlink'])) {
395 $link = $GLOBALS['userlink'];
396 } else {
397 return false;
401 if ($get_from_cache) {
402 return $GLOBALS['cached_affected_rows'];
403 } else {
404 return mysql_affected_rows($link);
409 * returns metainfo for fields in $result
411 * @todo add missing keys like in mysqli_query (decimals)
412 * @param resource $result
413 * @return array meta info for fields in $result
415 function PMA_DBI_get_fields_meta($result)
417 $fields = array();
418 $num_fields = mysql_num_fields($result);
419 for ($i = 0; $i < $num_fields; $i++) {
420 $field = mysql_fetch_field($result, $i);
421 $field->flags = mysql_field_flags($result, $i);
422 $field->orgtable = mysql_field_table($result, $i);
423 $field->orgname = mysql_field_name($result, $i);
424 $fields[] = $field;
426 return $fields;
430 * return number of fields in given $result
432 * @param resource $result
433 * @return int field count
435 function PMA_DBI_num_fields($result)
437 return mysql_num_fields($result);
441 * returns the length of the given field $i in $result
443 * @param resource $result
444 * @param int $i field
445 * @return int length of field
447 function PMA_DBI_field_len($result, $i)
449 return mysql_field_len($result, $i);
453 * returns name of $i. field in $result
455 * @param resource $result
456 * @param int $i field
457 * @return string name of $i. field in $result
459 function PMA_DBI_field_name($result, $i)
461 return mysql_field_name($result, $i);
465 * returns concatenated string of human readable field flags
467 * @param resource $result
468 * @param int $i field
469 * @return string field flags
471 function PMA_DBI_field_flags($result, $i)
473 return mysql_field_flags($result, $i);