2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the classic MySQL extension
6 * @package PhpMyAdmin-DBI
9 if (! defined('PHPMYADMIN')) {
13 require_once './libraries/logging.lib.php';
18 if (! defined('PMA_MYSQL_CLIENT_API')) {
19 $client_api = explode('.', mysql_get_client_info());
21 'PMA_MYSQL_CLIENT_API',
24 $client_api[0], $client_api[1], intval($client_api[2])
31 * Helper function for connecting to the database server
33 * @param array $server host/port/socket
34 * @param string $user mysql user name
35 * @param string $password mysql user password
36 * @param int $client_flags client flags of connection
37 * @param bool $persistent whether to use peristent connection
39 * @return mixed false on error or a mysql connection resource on success
41 function PMA_DBI_real_connect($server, $user, $password, $client_flags,
46 if (empty($client_flags)) {
47 if ($cfg['PersistentConnections'] ||
$persistent) {
48 $link = @mysql_pconnect
($server, $user, $password);
50 $link = @mysql_connect
($server, $user, $password);
53 if ($cfg['PersistentConnections'] ||
$persistent) {
54 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
56 $link = @mysql_connect
($server, $user, $password, false, $client_flags);
64 * Run the multi query and output the results
66 * @param mysqli $link mysqli object
67 * @param string $query multi query statement to execute
69 * @return boolean false always false since mysql extention not support
70 * for multi query executions
72 function PMA_DBI_real_multi_query($link, $query)
74 // N.B.: PHP's 'mysql' extension does not support
75 // multi_queries so this function will always
76 // return false. Use the 'mysqli' extension, if
77 // you need support for multi_queries.
82 * connects to the database server
84 * @param string $user mysql user name
85 * @param string $password mysql user password
86 * @param bool $is_controluser whether this is a control user connection
87 * @param array $server host/port/socket/persistent
88 * @param bool $auxiliary_connection (when true, don't go back to login if
91 * @return mixed false on error or a mysqli object on success
93 function PMA_DBI_connect(
94 $user, $password, $is_controluser = false, $server = null,
95 $auxiliary_connection = false
100 $server_port = (empty($server['port']))
102 : ':' . (int)$server['port'];
103 $server_socket = (empty($server['socket']))
105 : ':' . $server['socket'];
107 $server_port = (empty($cfg['Server']['port']))
109 : ':' . (int)$cfg['Server']['port'];
110 $server_socket = (empty($cfg['Server']['socket']))
112 : ':' . $cfg['Server']['socket'];
117 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
118 // for the case where the client library was not compiled
119 // with --enable-local-infile
120 $client_flags |
= 128;
122 /* Optionally compress connection */
123 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
124 $client_flags |
= MYSQL_CLIENT_COMPRESS
;
127 /* Optionally enable SSL */
128 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
129 $client_flags |
= MYSQL_CLIENT_SSL
;
133 $link = PMA_DBI_real_connect(
134 $cfg['Server']['host'] . $server_port . $server_socket,
135 $user, $password, empty($client_flags) ?
null : $client_flags
138 // Retry with empty password if we're allowed to
139 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
140 $link = PMA_DBI_real_connect(
141 $cfg['Server']['host'] . $server_port . $server_socket,
142 $user, '', empty($client_flags) ?
null : $client_flags
146 if (!isset($server['host'])) {
147 $link = PMA_DBI_real_connect($server_socket, $user, $password, null);
149 $link = PMA_DBI_real_connect(
150 $server['host'] . $server_port . $server_socket,
151 $user, $password, null
156 if ($is_controluser) {
159 'Connection for controluser as defined'
160 . ' in your configuration failed.'
166 // we could be calling PMA_DBI_connect() to connect to another
167 // server, for example in the Synchronize feature, so do not
168 // go back to main login if it fails
169 if (! $auxiliary_connection) {
170 PMA_log_user($user, 'mysql-denied');
172 $auth_plugin->authFails();
178 PMA_DBI_postConnect($link, $is_controluser);
184 * selects given database
186 * @param string $dbname name of db to select
187 * @param resource $link mysql link resource
191 function PMA_DBI_select_db($dbname, $link = null)
194 if (isset($GLOBALS['userlink'])) {
195 $link = $GLOBALS['userlink'];
200 return mysql_select_db($dbname, $link);
204 * runs a query and returns the result
206 * @param string $query query to run
207 * @param resource $link mysql link resource
208 * @param int $options query options
212 function PMA_DBI_real_query($query, $link, $options)
214 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
215 return mysql_query($query, $link);
216 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
217 return mysql_unbuffered_query($query, $link);
219 return mysql_query($query, $link);
224 * returns array of rows with associative and numeric keys from $result
226 * @param resource $result result MySQL result
230 function PMA_DBI_fetch_array($result)
232 return mysql_fetch_array($result, MYSQL_BOTH
);
236 * returns array of rows with associative keys from $result
238 * @param resource $result MySQL result
242 function PMA_DBI_fetch_assoc($result)
244 return mysql_fetch_array($result, MYSQL_ASSOC
);
248 * returns array of rows with numeric keys from $result
250 * @param resource $result MySQL result
254 function PMA_DBI_fetch_row($result)
256 return mysql_fetch_array($result, MYSQL_NUM
);
260 * Adjusts the result pointer to an arbitrary row in the result
262 * @param resource $result database result
263 * @param integer $offset offset to seek
265 * @return bool true on success, false on failure
267 function PMA_DBI_data_seek($result, $offset)
269 return mysql_data_seek($result, $offset);
273 * Frees memory associated with the result
275 * @param resource $result database result
279 function PMA_DBI_free_result($result)
281 if (is_resource($result) && get_resource_type($result) === 'mysql result') {
282 mysql_free_result($result);
287 * Check if there are any more query results from a multi query
291 function PMA_DBI_more_results()
293 // N.B.: PHP's 'mysql' extension does not support
294 // multi_queries so this function will always
295 // return false. Use the 'mysqli' extension, if
296 // you need support for multi_queries.
301 * Prepare next result from multi_query
305 function PMA_DBI_next_result()
307 // N.B.: PHP's 'mysql' extension does not support
308 // multi_queries so this function will always
309 // return false. Use the 'mysqli' extension, if
310 // you need support for multi_queries.
315 * Returns a string representing the type of connection used
317 * @param resource $link mysql link
319 * @return string type of connection used
321 function PMA_DBI_get_host_info($link = null)
323 if (null === $link) {
324 if (isset($GLOBALS['userlink'])) {
325 $link = $GLOBALS['userlink'];
330 return mysql_get_host_info($link);
334 * Returns the version of the MySQL protocol used
336 * @param resource $link mysql link
338 * @return int version of the MySQL protocol used
340 function PMA_DBI_get_proto_info($link = null)
342 if (null === $link) {
343 if (isset($GLOBALS['userlink'])) {
344 $link = $GLOBALS['userlink'];
349 return mysql_get_proto_info($link);
353 * returns a string that represents the client library version
355 * @return string MySQL client library version
357 function PMA_DBI_get_client_info()
359 return mysql_get_client_info();
363 * returns last error message or false if no errors occured
365 * @param resource $link mysql link
367 * @return string|bool $error or false
369 function PMA_DBI_getError($link = null)
371 $GLOBALS['errno'] = 0;
373 /* Treat false same as null because of controllink */
374 if ($link === false) {
378 if (null === $link && isset($GLOBALS['userlink'])) {
379 $link =& $GLOBALS['userlink'];
381 // Do not stop now. On the initial connection, we don't have a $link,
382 // we don't have a $GLOBALS['userlink'], but we can catch the error code
387 if (null !== $link && false !== $link) {
388 $error_number = mysql_errno($link);
389 $error_message = mysql_error($link);
391 $error_number = mysql_errno();
392 $error_message = mysql_error();
394 if (0 == $error_number) {
398 // keep the error number for further check after the call to PMA_DBI_getError()
399 $GLOBALS['errno'] = $error_number;
401 return PMA_DBI_formatError($error_number, $error_message);
405 * returns the number of rows returned by last query
407 * @param resource $result MySQL result
411 function PMA_DBI_num_rows($result)
413 if (!is_bool($result)) {
414 return mysql_num_rows($result);
421 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
423 * @param resource $link the mysql object
427 function PMA_DBI_insert_id($link = null)
430 if (isset($GLOBALS['userlink'])) {
431 $link = $GLOBALS['userlink'];
436 // If the primary key is BIGINT we get an incorrect result
437 // (sometimes negative, sometimes positive)
438 // and in the present function we don't know if the PK is BIGINT
439 // so better play safe and use LAST_INSERT_ID()
441 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
445 * returns the number of rows affected by last query
447 * @param resource $link the mysql object
448 * @param bool $get_from_cache whether to retrieve from cache
452 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
455 if (isset($GLOBALS['userlink'])) {
456 $link = $GLOBALS['userlink'];
462 if ($get_from_cache) {
463 return $GLOBALS['cached_affected_rows'];
465 return mysql_affected_rows($link);
470 * returns metainfo for fields in $result
472 * @param resource $result MySQL result
474 * @return array meta info for fields in $result
476 * @todo add missing keys like in mysqli_query (decimals)
478 function PMA_DBI_get_fields_meta($result)
481 $num_fields = mysql_num_fields($result);
482 for ($i = 0; $i < $num_fields; $i++
) {
483 $field = mysql_fetch_field($result, $i);
484 $field->flags
= mysql_field_flags($result, $i);
485 $field->orgtable
= mysql_field_table($result, $i);
486 $field->orgname
= mysql_field_name($result, $i);
493 * return number of fields in given $result
495 * @param resource $result MySQL result
497 * @return int field count
499 function PMA_DBI_num_fields($result)
501 return mysql_num_fields($result);
505 * returns the length of the given field $i in $result
507 * @param resource $result MySQL result
508 * @param int $i field
510 * @return int length of field
512 function PMA_DBI_field_len($result, $i)
514 return mysql_field_len($result, $i);
518 * returns name of $i. field in $result
520 * @param resource $result MySQL result
521 * @param int $i field
523 * @return string name of $i. field in $result
525 function PMA_DBI_field_name($result, $i)
527 return mysql_field_name($result, $i);
531 * returns concatenated string of human readable field flags
533 * @param resource $result MySQL result
534 * @param int $i field
536 * @return string field flags
538 function PMA_DBI_field_flags($result, $i)
540 return mysql_field_flags($result, $i);
544 * Store the result returned from multi query
548 function PMA_DBI_store_result()