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])));
23 function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistent=false)
27 if (empty($client_flags)) {
28 if ($cfg['PersistentConnections'] ||
$persistent) {
29 $link = @mysql_pconnect
($server, $user, $password);
31 $link = @mysql_connect
($server, $user, $password);
34 if ($cfg['PersistentConnections'] ||
$persistent) {
35 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
37 $link = @mysql_connect
($server, $user, $password, false, $client_flags);
45 * @param string $user mysql user name
46 * @param string $password mysql user password
47 * @param boolean $is_controluser
48 * @param array $server host/port/socket/persistent
49 * @param boolean $auxiliary_connection (when true, don't go back to login if connection fails)
50 * @return mixed false on error or a mysqli object on success
52 function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
54 global $cfg, $php_errormsg;
57 $server_port = (empty($server['port']))
59 : ':' . (int)$server['port'];
60 $server_socket = (empty($server['socket']))
62 : ':' . $server['socket'];
63 $server_persistent = (empty($server['persistent']))
67 $server_port = (empty($cfg['Server']['port']))
69 : ':' . (int)$cfg['Server']['port'];
70 $server_socket = (empty($cfg['Server']['socket']))
72 : ':' . $cfg['Server']['socket'];
75 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
76 $cfg['Server']['socket'] = '';
81 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
82 // for the case where the client library was not compiled
83 // with --enable-local-infile
86 /* Optionally compress connection */
87 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
88 $client_flags |
= MYSQL_CLIENT_COMPRESS
;
91 /* Optionally enable SSL */
92 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
93 $client_flags |
= MYSQL_CLIENT_SSL
;
97 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ?
NULL : $client_flags);
99 // Retry with empty password if we're allowed to
100 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
101 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ?
NULL : $client_flags);
104 if (!isset($server['host'])) {
105 $link = PMA_DBI_real_connect($server_socket, $user, $password, NULL, $server_persistent);
107 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, NULL, $server_persistent);
111 if ($is_controluser) {
112 trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING
);
115 // we could be calling PMA_DBI_connect() to connect to another
116 // server, for example in the Synchronize feature, so do not
117 // go back to main login if it fails
118 if (! $auxiliary_connection) {
119 PMA_log_user($user, 'mysql-denied');
126 PMA_DBI_postConnect($link, $is_controluser);
134 * @param string $dbname name of db to select
135 * @param resource $link mysql link resource
136 * @return boolean success
138 function PMA_DBI_select_db($dbname, $link = null)
141 if (isset($GLOBALS['userlink'])) {
142 $link = $GLOBALS['userlink'];
147 return mysql_select_db($dbname, $link);
151 * runs a query and returns the result
153 * @param string $query query to run
154 * @param resource $link mysql link resource
155 * @param integer $options
158 function PMA_DBI_real_query($query, $link, $options)
160 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
161 return mysql_query($query, $link);
162 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
163 return mysql_unbuffered_query($query, $link);
165 return mysql_query($query, $link);
169 function PMA_DBI_fetch_array($result)
171 return mysql_fetch_array($result, MYSQL_BOTH
);
174 function PMA_DBI_fetch_assoc($result) {
175 return mysql_fetch_array($result, MYSQL_ASSOC
);
178 function PMA_DBI_fetch_row($result)
180 return mysql_fetch_array($result, MYSQL_NUM
);
184 * Adjusts the result pointer to an arbitrary row in the result
188 * @return boolean true on success, false on failure
190 function PMA_DBI_data_seek($result, $offset)
192 return mysql_data_seek($result, $offset);
196 * Frees the memory associated with the results
198 * @param result $result,... one or more mysql result resources
200 function PMA_DBI_free_result()
202 foreach (func_get_args() as $result) {
203 if (is_resource($result)
204 && get_resource_type($result) === 'mysql result') {
205 mysql_free_result($result);
211 * Check if there are any more query results from a multi query
213 * @return boolean false
215 function PMA_DBI_more_results() {
216 // N.B.: PHP's 'mysql' extension does not support
217 // multi_queries so this function will always
218 // return false. Use the 'mysqli' extension, if
219 // you need support for multi_queries.
224 * Prepare next result from multi_query
226 * @return boolean false
228 function PMA_DBI_next_result() {
229 // N.B.: PHP's 'mysql' extension does not support
230 // multi_queries so this function will always
231 // return false. Use the 'mysqli' extension, if
232 // you need support for multi_queries.
237 * Returns a string representing the type of connection used
238 * @param resource $link mysql link
239 * @return string type of connection used
241 function PMA_DBI_get_host_info($link = null)
243 if (null === $link) {
244 if (isset($GLOBALS['userlink'])) {
245 $link = $GLOBALS['userlink'];
250 return mysql_get_host_info($link);
254 * Returns the version of the MySQL protocol used
255 * @param resource $link mysql link
256 * @return integer version of the MySQL protocol used
258 function PMA_DBI_get_proto_info($link = null)
260 if (null === $link) {
261 if (isset($GLOBALS['userlink'])) {
262 $link = $GLOBALS['userlink'];
267 return mysql_get_proto_info($link);
271 * returns a string that represents the client library version
272 * @return string MySQL client library version
274 function PMA_DBI_get_client_info()
276 return mysql_get_client_info();
280 * returns last error message or false if no errors occured
282 * @param resource $link mysql link
283 * @return string|boolean $error or false
285 function PMA_DBI_getError($link = null)
287 $GLOBALS['errno'] = 0;
289 /* Treat false same as null because of controllink */
290 if ($link === false) {
294 if (null === $link && isset($GLOBALS['userlink'])) {
295 $link =& $GLOBALS['userlink'];
297 // Do not stop now. On the initial connection, we don't have a $link,
298 // we don't have a $GLOBALS['userlink'], but we can catch the error code
303 if (null !== $link && false !== $link) {
304 $error_number = mysql_errno($link);
305 $error_message = mysql_error($link);
307 $error_number = mysql_errno();
308 $error_message = mysql_error();
310 if (0 == $error_number) {
314 // keep the error number for further check after the call to PMA_DBI_getError()
315 $GLOBALS['errno'] = $error_number;
317 if (! empty($error_message)) {
318 $error_message = PMA_DBI_convert_message($error_message);
321 $error_message = htmlspecialchars($error_message);
323 // Some errors messages cannot be obtained by mysql_error()
324 if ($error_number == 2002) {
325 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding') . ' ' . __('(or the local MySQL server\'s socket is not correctly configured)');
326 } elseif ($error_number == 2003) {
327 $error = '#' . ((string) $error_number) . ' - ' . __('The server is not responding');
328 } elseif ($error_number == 1005) {
329 /* InnoDB contraints, see
330 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
332 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
333 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
334 '">' . __('Details...') . '</a>)';
336 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
341 function PMA_DBI_num_rows($result)
343 if (!is_bool($result)) {
344 return mysql_num_rows($result);
350 function PMA_DBI_insert_id($link = null)
353 if (isset($GLOBALS['userlink'])) {
354 $link = $GLOBALS['userlink'];
359 // If the primary key is BIGINT we get an incorrect result
360 // (sometimes negative, sometimes positive)
361 // and in the present function we don't know if the PK is BIGINT
362 // so better play safe and use LAST_INSERT_ID()
364 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
368 * returns the number of rows affected by last query
370 * @param object mysql $link the mysql object
371 * @param boolean $get_from_cache
372 * @return string integer
374 function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
377 if (isset($GLOBALS['userlink'])) {
378 $link = $GLOBALS['userlink'];
384 if ($get_from_cache) {
385 return $GLOBALS['cached_affected_rows'];
387 return mysql_affected_rows($link);
392 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
394 function PMA_DBI_get_fields_meta($result)
397 $num_fields = mysql_num_fields($result);
398 for ($i = 0; $i < $num_fields; $i++
) {
399 $fields[] = mysql_fetch_field($result, $i);
404 function PMA_DBI_num_fields($result)
406 return mysql_num_fields($result);
409 function PMA_DBI_field_len($result, $i)
411 return mysql_field_len($result, $i);
414 function PMA_DBI_field_name($result, $i)
416 return mysql_field_name($result, $i);
419 function PMA_DBI_field_flags($result, $i)
421 return mysql_field_flags($result, $i);