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 boolean $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 boolean $is_controluser
60 * @param array $server host/port/socket/persistent
61 * @param boolean $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, $php_errormsg;
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 integer $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) {
192 return mysql_fetch_array($result, MYSQL_ASSOC
);
196 * returns array of rows with numeric keys from $result
198 * @param resource $result
201 function PMA_DBI_fetch_row($result)
203 return mysql_fetch_array($result, MYSQL_NUM
);
207 * Adjusts the result pointer to an arbitrary row in the result
211 * @return boolean true on success, false on failure
213 function PMA_DBI_data_seek($result, $offset)
215 return mysql_data_seek($result, $offset);
219 * Frees the memory associated with the results
221 * @param result $result,... one or more mysql result resources
223 function PMA_DBI_free_result()
225 foreach (func_get_args() as $result) {
226 if (is_resource($result)
227 && get_resource_type($result) === 'mysql result') {
228 mysql_free_result($result);
234 * Check if there are any more query results from a multi query
236 * @return boolean false
238 function PMA_DBI_more_results() {
239 // N.B.: PHP's 'mysql' extension does not support
240 // multi_queries so this function will always
241 // return false. Use the 'mysqli' extension, if
242 // you need support for multi_queries.
247 * Prepare next result from multi_query
249 * @return boolean false
251 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
261 * @param resource $link mysql link
262 * @return string type of connection used
264 function PMA_DBI_get_host_info($link = null)
266 if (null === $link) {
267 if (isset($GLOBALS['userlink'])) {
268 $link = $GLOBALS['userlink'];
273 return mysql_get_host_info($link);
277 * Returns the version of the MySQL protocol used
278 * @param resource $link mysql link
279 * @return integer version of the MySQL protocol used
281 function PMA_DBI_get_proto_info($link = null)
283 if (null === $link) {
284 if (isset($GLOBALS['userlink'])) {
285 $link = $GLOBALS['userlink'];
290 return mysql_get_proto_info($link);
294 * returns a string that represents the client library version
295 * @return string MySQL client library version
297 function PMA_DBI_get_client_info()
299 return mysql_get_client_info();
303 * returns last error message or false if no errors occured
305 * @param resource $link mysql link
306 * @return string|boolean $error or false
308 function PMA_DBI_getError($link = null)
310 $GLOBALS['errno'] = 0;
312 /* Treat false same as null because of controllink */
313 if ($link === false) {
317 if (null === $link && isset($GLOBALS['userlink'])) {
318 $link =& $GLOBALS['userlink'];
320 // Do not stop now. On the initial connection, we don't have a $link,
321 // we don't have a $GLOBALS['userlink'], but we can catch the error code
326 if (null !== $link && false !== $link) {
327 $error_number = mysql_errno($link);
328 $error_message = mysql_error($link);
330 $error_number = mysql_errno();
331 $error_message = mysql_error();
333 if (0 == $error_number) {
337 // keep the error number for further check after the call to PMA_DBI_getError()
338 $GLOBALS['errno'] = $error_number;
340 if (! empty($error_message)) {
341 $error_message = PMA_DBI_convert_message($error_message);
344 $error_message = htmlspecialchars($error_message);
346 // Some errors messages cannot be obtained by mysql_error()
347 if ($error_number == 2002) {
348 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
349 } elseif ($error_number == 2003) {
350 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding');
351 } elseif ($error_number == 1005) {
352 /* InnoDB contraints, see
353 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
355 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
356 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
357 '">' . __('Details...') . '</a>)';
359 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
365 * returns the number of rows returned by last query
367 * @param resource $result
368 * @return string|ineteger
370 function PMA_DBI_num_rows($result)
372 if (!is_bool($result)) {
373 return mysql_num_rows($result);
380 * returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
382 * @param resource $link the mysql object
383 * @return string|ineteger
385 function PMA_DBI_insert_id($link = null)
388 if (isset($GLOBALS['userlink'])) {
389 $link = $GLOBALS['userlink'];
394 // If the primary key is BIGINT we get an incorrect result
395 // (sometimes negative, sometimes positive)
396 // and in the present function we don't know if the PK is BIGINT
397 // so better play safe and use LAST_INSERT_ID()
399 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
403 * returns the number of rows affected by last query
405 * @param resource $link the mysql object
406 * @param boolean $get_from_cache
407 * @return string|integer
409 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
412 if (isset($GLOBALS['userlink'])) {
413 $link = $GLOBALS['userlink'];
419 if ($get_from_cache) {
420 return $GLOBALS['cached_affected_rows'];
422 return mysql_affected_rows($link);
427 * returns metainfo for fields in $result
429 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
430 * @param resource $result
431 * @return array meta info for fields in $result
433 function PMA_DBI_get_fields_meta($result)
436 $num_fields = mysql_num_fields($result);
437 for ($i = 0; $i < $num_fields; $i++
) {
438 $fields[] = mysql_fetch_field($result, $i);
443 function PMA_DBI_num_fields($result)
445 return mysql_num_fields($result);
448 function PMA_DBI_field_len($result, $i)
450 return mysql_field_len($result, $i);
453 function PMA_DBI_field_name($result, $i)
455 return mysql_field_name($result, $i);
458 function PMA_DBI_field_flags($result, $i)
460 return mysql_field_flags($result, $i);