2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the classic MySQL extension
6 * @package PhpMyAdmin-DBI-MySQL
8 if (! defined('PHPMYADMIN')) {
12 require_once './libraries/logging.lib.php';
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])));
24 * Helper function for connecting to the database server
26 * @param string $server
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)
37 if (empty($client_flags)) {
38 if ($cfg['PersistentConnections'] ||
$persistent) {
39 $link = @mysql_pconnect
($server, $user, $password);
41 $link = @mysql_connect
($server, $user, $password);
44 if ($cfg['PersistentConnections'] ||
$persistent) {
45 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
47 $link = @mysql_connect
($server, $user, $password, false, $client_flags);
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)
69 $server_port = (empty($server['port']))
71 : ':' . (int)$server['port'];
72 $server_socket = (empty($server['socket']))
74 : ':' . $server['socket'];
76 $server_port = (empty($cfg['Server']['port']))
78 : ':' . (int)$cfg['Server']['port'];
79 $server_socket = (empty($cfg['Server']['socket']))
81 : ':' . $cfg['Server']['socket'];
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
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
;
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);
109 if (!isset($server['host'])) {
110 $link = PMA_DBI_real_connect($server_socket, $user, $password, null);
112 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, null);
116 if ($is_controluser) {
117 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING
);
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');
131 PMA_DBI_postConnect($link, $is_controluser);
137 * selects given database
139 * @param string $dbname name of db to select
140 * @param resource $link mysql link resource
143 function PMA_DBI_select_db($dbname, $link = null)
146 if (isset($GLOBALS['userlink'])) {
147 $link = $GLOBALS['userlink'];
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
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);
170 return mysql_query($query, $link);
175 * returns array of rows with associative and numeric keys from $result
177 * @param resource $result
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
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
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
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
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.
246 * Prepare next result from multi_query
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.
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'];
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'];
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) {
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
329 if (null !== $link && false !== $link) {
330 $error_number = mysql_errno($link);
331 $error_message = mysql_error($link);
333 $error_number = mysql_errno();
334 $error_message = mysql_error();
336 if (0 == $error_number) {
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
352 function PMA_DBI_num_rows($result)
354 if (!is_bool($result)) {
355 return mysql_num_rows($result);
362 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
364 * @param resource $link the mysql object
367 function PMA_DBI_insert_id($link = null)
370 if (isset($GLOBALS['userlink'])) {
371 $link = $GLOBALS['userlink'];
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
391 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
394 if (isset($GLOBALS['userlink'])) {
395 $link = $GLOBALS['userlink'];
401 if ($get_from_cache) {
402 return $GLOBALS['cached_affected_rows'];
404 return mysql_affected_rows($link);
409 * returns metainfo for fields in $result
411 * @todo add missing keys like in mysqli_query (orgname, orgtable, flags, decimals)
412 * @param resource $result
413 * @return array meta info for fields in $result
415 function PMA_DBI_get_fields_meta($result)
418 $num_fields = mysql_num_fields($result);
419 for ($i = 0; $i < $num_fields; $i++
) {
420 $fields[] = mysql_fetch_field($result, $i);
426 * return number of fields in given $result
428 * @param resource $result
429 * @return int field count
431 function PMA_DBI_num_fields($result)
433 return mysql_num_fields($result);
437 * returns the length of the given field $i in $result
439 * @param resource $result
440 * @param int $i field
441 * @return int length of field
443 function PMA_DBI_field_len($result, $i)
445 return mysql_field_len($result, $i);
449 * returns name of $i. field in $result
451 * @param resource $result
452 * @param int $i field
453 * @return string name of $i. field in $result
455 function PMA_DBI_field_name($result, $i)
457 return mysql_field_name($result, $i);
461 * returns concatenated string of human readable field flags
463 * @param resource $result
464 * @param int $i field
465 * @return string field flags
467 function PMA_DBI_field_flags($result, $i)
469 return mysql_field_flags($result, $i);