2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the classic MySQL extension
6 * @package phpMyAdmin-DBI-MySQL
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());
20 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
24 function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistant=false)
28 if (empty($client_flags)) {
29 if ($cfg['PersistentConnections'] ||
$persistant) {
30 $link = @mysql_pconnect
($server, $user, $password);
32 $link = @mysql_connect
($server, $user, $password);
35 if ($cfg['PersistentConnections'] ||
$persistant) {
36 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
38 $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/persistant
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_persistant = (empty($server['persistant']))
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_persistant);
107 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, NULL, $server_persistant);
111 if ($is_controluser) {
112 trigger_error($GLOBALS['strControluserFailed'], 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_try_query($query, $link = null, $options = 0)
161 if (isset($GLOBALS['userlink'])) {
162 $link = $GLOBALS['userlink'];
168 if ($GLOBALS['cfg']['DBG']['sql']) {
169 $time = microtime(true);
171 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
172 $r = mysql_query($query, $link);
173 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
174 $r = mysql_unbuffered_query($query, $link);
176 $r = mysql_query($query, $link);
179 if ($GLOBALS['cfg']['DBG']['sql']) {
180 $time = microtime(true) - $time;
184 if (isset($_SESSION['debug']['queries'][$hash])) {
185 $_SESSION['debug']['queries'][$hash]['count']++
;
187 $_SESSION['debug']['queries'][$hash] = array();
188 $_SESSION['debug']['queries'][$hash]['count'] = 1;
189 $_SESSION['debug']['queries'][$hash]['query'] = $query;
190 $_SESSION['debug']['queries'][$hash]['time'] = $time;
194 foreach (debug_backtrace() as $trace_step) {
195 $trace[] = PMA_Error
::relPath($trace_step['file']) . '#'
196 . $trace_step['line'] . ': '
197 . (isset($trace_step['class']) ?
$trace_step['class'] : '')
198 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
199 . (isset($trace_step['type']) ?
$trace_step['type'] : '')
200 . (isset($trace_step['function']) ?
$trace_step['function'] : '')
202 . (isset($trace_step['params']) ?
implode(', ', $trace_step['params']) : '')
206 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
208 if ($r != FALSE && PMA_Tracker
::isActive() == TRUE ) {
209 PMA_Tracker
::handleQuery($query);
215 function PMA_DBI_fetch_array($result)
217 return mysql_fetch_array($result, MYSQL_BOTH
);
220 function PMA_DBI_fetch_assoc($result) {
221 return mysql_fetch_array($result, MYSQL_ASSOC
);
224 function PMA_DBI_fetch_row($result)
226 return mysql_fetch_array($result, MYSQL_NUM
);
230 * Adjusts the result pointer to an arbitrary row in the result
232 * @uses mysql_data_seek()
235 * @return boolean true on success, false on failure
237 function PMA_DBI_data_seek($result, $offset)
239 return mysql_data_seek($result, $offset);
243 * Frees the memory associated with the results
245 * @param result $result,... one or more mysql result resources
247 function PMA_DBI_free_result()
249 foreach (func_get_args() as $result) {
250 if (is_resource($result)
251 && get_resource_type($result) === 'mysql result') {
252 mysql_free_result($result);
258 * Returns a string representing the type of connection used
259 * @uses mysql_get_host_info()
260 * @uses $GLOBALS['userlink'] as default for $link
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 * @uses mysql_get_proto_info()
279 * @uses $GLOBALS['userlink'] as default for $link
280 * @param resource $link mysql link
281 * @return integer 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
297 * @uses mysql_get_client_info()
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 * @uses PMA_DBI_convert_message()
309 * @uses $GLOBALS['errno']
310 * @uses $GLOBALS['userlink']
311 * @uses $GLOBALS['strServerNotResponding']
312 * @uses $GLOBALS['strSocketProblem']
313 * @uses $GLOBALS['strDetails']
314 * @uses mysql_errno()
315 * @uses mysql_error()
317 * @uses PMA_generate_common_url()
318 * @param resource $link mysql link
319 * @return string|boolean $error or false
321 function PMA_DBI_getError($link = null)
323 $GLOBALS['errno'] = 0;
324 if (null === $link && isset($GLOBALS['userlink'])) {
325 $link =& $GLOBALS['userlink'];
327 // Do not stop now. On the initial connection, we don't have a $link,
328 // we don't have a $GLOBALS['userlink'], but we can catch the error code
333 if (null !== $link && false !== $link) {
334 $error_number = mysql_errno($link);
335 $error_message = mysql_error($link);
337 $error_number = mysql_errno();
338 $error_message = mysql_error();
340 if (0 == $error_number) {
344 // keep the error number for further check after the call to PMA_DBI_getError()
345 $GLOBALS['errno'] = $error_number;
347 if (! empty($error_message)) {
348 $error_message = PMA_DBI_convert_message($error_message);
351 // Some errors messages cannot be obtained by mysql_error()
352 if ($error_number == 2002) {
353 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
354 } elseif ($error_number == 2003) {
355 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
356 } elseif ($error_number == 1005) {
357 /* InnoDB contraints, see
358 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
360 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
361 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
362 '">' . $GLOBALS['strDetails'] . '</a>)';
364 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
369 function PMA_DBI_num_rows($result)
371 if (!is_bool($result)) {
372 return mysql_num_rows($result);
378 function PMA_DBI_insert_id($link = null)
381 if (isset($GLOBALS['userlink'])) {
382 $link = $GLOBALS['userlink'];
387 //$insert_id = mysql_insert_id($link);
388 // if the primary key is BIGINT we get an incorrect result
389 // (sometimes negative, sometimes positive)
390 // and in the present function we don't know if the PK is BIGINT
391 // so better play safe and use LAST_INSERT_ID()
393 // by the way, no problem with mysqli_insert_id()
394 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
397 function PMA_DBI_affected_rows($link = null)
400 if (isset($GLOBALS['userlink'])) {
401 $link = $GLOBALS['userlink'];
406 return mysql_affected_rows($link);
410 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
412 function PMA_DBI_get_fields_meta($result)
415 $num_fields = mysql_num_fields($result);
416 for ($i = 0; $i < $num_fields; $i++
) {
417 $fields[] = mysql_fetch_field($result, $i);
422 function PMA_DBI_num_fields($result)
424 return mysql_num_fields($result);
427 function PMA_DBI_field_len($result, $i)
429 return mysql_field_len($result, $i);
432 function PMA_DBI_field_name($result, $i)
434 return mysql_field_name($result, $i);
437 function PMA_DBI_field_flags($result, $i)
439 return mysql_field_flags($result, $i);