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)
28 if (empty($client_flags)) {
29 if ($cfg['PersistentConnections']) {
30 $link = @mysql_pconnect
($server, $user, $password);
32 $link = @mysql_connect
($server, $user, $password);
35 if ($cfg['PersistentConnections']) {
36 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
38 $link = @mysql_connect
($server, $user, $password, false, $client_flags);
45 function PMA_DBI_connect($user, $password, $is_controluser = false)
47 global $cfg, $php_errormsg;
49 $server_port = (empty($cfg['Server']['port']))
51 : ':' . $cfg['Server']['port'];
53 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
54 $cfg['Server']['socket'] = '';
57 $server_socket = (empty($cfg['Server']['socket']))
59 : ':' . $cfg['Server']['socket'];
63 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
64 // for the case where the client library was not compiled
65 // with --enable-local-infile
68 /* Optionally compress connection */
69 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
70 $client_flags |
= MYSQL_CLIENT_COMPRESS
;
73 /* Optionally enable SSL */
74 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
75 $client_flags |
= MYSQL_CLIENT_SSL
;
78 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ?
NULL : $client_flags);
80 // Retry with empty password if we're allowed to
81 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
82 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ?
NULL : $client_flags);
86 if ($is_controluser) {
87 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING
);
90 PMA_log_user($user, 'mysql-denied');
94 PMA_DBI_postConnect($link, $is_controluser);
102 * @param string $dbname name of db to select
103 * @param resource $link mysql link resource
104 * @return boolean success
106 function PMA_DBI_select_db($dbname, $link = null)
109 if (isset($GLOBALS['userlink'])) {
110 $link = $GLOBALS['userlink'];
115 return mysql_select_db($dbname, $link);
119 * runs a query and returns the result
121 * @param string $query query to run
122 * @param resource $link mysql link resource
123 * @param integer $options
126 function PMA_DBI_try_query($query, $link = null, $options = 0)
129 if (isset($GLOBALS['userlink'])) {
130 $link = $GLOBALS['userlink'];
136 if ($GLOBALS['cfg']['DBG']['sql']) {
137 $time = microtime(true);
139 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
140 $r = mysql_query($query, $link);
141 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
142 $r = mysql_unbuffered_query($query, $link);
144 $r = mysql_query($query, $link);
147 if ($GLOBALS['cfg']['DBG']['sql']) {
148 $time = microtime(true) - $time;
152 if (isset($_SESSION['debug']['queries'][$hash])) {
153 $_SESSION['debug']['queries'][$hash]['count']++
;
155 $_SESSION['debug']['queries'][$hash] = array();
156 $_SESSION['debug']['queries'][$hash]['count'] = 1;
157 $_SESSION['debug']['queries'][$hash]['query'] = $query;
158 $_SESSION['debug']['queries'][$hash]['time'] = $time;
162 foreach (debug_backtrace() as $trace_step) {
163 $trace[] = PMA_Error
::relPath($trace_step['file']) . '#'
164 . $trace_step['line'] . ': '
165 . (isset($trace_step['class']) ?
$trace_step['class'] : '')
166 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
167 . (isset($trace_step['type']) ?
$trace_step['type'] : '')
168 . (isset($trace_step['function']) ?
$trace_step['function'] : '')
170 . (isset($trace_step['params']) ?
implode(', ', $trace_step['params']) : '')
174 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
180 function PMA_DBI_fetch_array($result)
182 return mysql_fetch_array($result, MYSQL_BOTH
);
185 function PMA_DBI_fetch_assoc($result) {
186 return mysql_fetch_array($result, MYSQL_ASSOC
);
189 function PMA_DBI_fetch_row($result)
191 return mysql_fetch_array($result, MYSQL_NUM
);
195 * Adjusts the result pointer to an arbitrary row in the result
197 * @uses mysql_data_seek()
200 * @return boolean true on success, false on failure
202 function PMA_DBI_data_seek($result, $offset)
204 return mysql_data_seek($result, $offset);
208 * Frees the memory associated with the results
210 * @param result $result,... one or more mysql result resources
212 function PMA_DBI_free_result()
214 foreach (func_get_args() as $result) {
215 if (is_resource($result)
216 && get_resource_type($result) === 'mysql result') {
217 mysql_free_result($result);
223 * Returns a string representing the type of connection used
224 * @uses mysql_get_host_info()
225 * @uses $GLOBALS['userlink'] as default for $link
226 * @param resource $link mysql link
227 * @return string type of connection used
229 function PMA_DBI_get_host_info($link = null)
231 if (null === $link) {
232 if (isset($GLOBALS['userlink'])) {
233 $link = $GLOBALS['userlink'];
238 return mysql_get_host_info($link);
242 * Returns the version of the MySQL protocol used
243 * @uses mysql_get_proto_info()
244 * @uses $GLOBALS['userlink'] as default for $link
245 * @param resource $link mysql link
246 * @return integer version of the MySQL protocol used
248 function PMA_DBI_get_proto_info($link = null)
250 if (null === $link) {
251 if (isset($GLOBALS['userlink'])) {
252 $link = $GLOBALS['userlink'];
257 return mysql_get_proto_info($link);
261 * returns a string that represents the client library version
262 * @uses mysql_get_client_info()
263 * @return string MySQL client library version
265 function PMA_DBI_get_client_info()
267 return mysql_get_client_info();
271 * returns last error message or false if no errors occured
273 * @uses PMA_DBI_convert_message()
274 * @uses $GLOBALS['errno']
275 * @uses $GLOBALS['userlink']
276 * @uses $GLOBALS['strServerNotResponding']
277 * @uses $GLOBALS['strSocketProblem']
278 * @uses $GLOBALS['strDetails']
279 * @uses mysql_errno()
280 * @uses mysql_error()
282 * @uses PMA_generate_common_url()
283 * @param resource $link mysql link
284 * @return string|boolean $error or false
286 function PMA_DBI_getError($link = null)
288 $GLOBALS['errno'] = 0;
289 if (null === $link && isset($GLOBALS['userlink'])) {
290 $link =& $GLOBALS['userlink'];
292 // Do not stop now. On the initial connection, we don't have a $link,
293 // we don't have a $GLOBALS['userlink'], but we can catch the error code
298 if (null !== $link && false !== $link) {
299 $error_number = mysql_errno($link);
300 $error_message = mysql_error($link);
302 $error_number = mysql_errno();
303 $error_message = mysql_error();
305 if (0 == $error_number) {
309 // keep the error number for further check after the call to PMA_DBI_getError()
310 $GLOBALS['errno'] = $error_number;
312 if (! empty($error_message)) {
313 $error_message = PMA_DBI_convert_message($error_message);
316 // Some errors messages cannot be obtained by mysql_error()
317 if ($error_number == 2002) {
318 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
319 } elseif ($error_number == 2003) {
320 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
321 } elseif ($error_number == 1005) {
322 /* InnoDB contraints, see
323 * http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
325 $error = '#' . ((string) $error_number) . ' - ' . $error_message .
326 ' (<a href="server_engines.php' . PMA_generate_common_url(array('engine' => 'InnoDB', 'page' => 'Status')).
327 '">' . $GLOBALS['strDetails'] . '</a>)';
329 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
334 function PMA_DBI_num_rows($result)
336 if (!is_bool($result)) {
337 return mysql_num_rows($result);
343 function PMA_DBI_insert_id($link = null)
346 if (isset($GLOBALS['userlink'])) {
347 $link = $GLOBALS['userlink'];
352 //$insert_id = mysql_insert_id($link);
353 // if the primary key is BIGINT we get an incorrect result
354 // (sometimes negative, sometimes positive)
355 // and in the present function we don't know if the PK is BIGINT
356 // so better play safe and use LAST_INSERT_ID()
358 // by the way, no problem with mysqli_insert_id()
359 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
362 function PMA_DBI_affected_rows($link = null)
365 if (isset($GLOBALS['userlink'])) {
366 $link = $GLOBALS['userlink'];
371 return mysql_affected_rows($link);
375 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
377 function PMA_DBI_get_fields_meta($result)
380 $num_fields = mysql_num_fields($result);
381 for ($i = 0; $i < $num_fields; $i++
) {
382 $fields[] = mysql_fetch_field($result, $i);
387 function PMA_DBI_num_fields($result)
389 return mysql_num_fields($result);
392 function PMA_DBI_field_len($result, $i)
394 return mysql_field_len($result, $i);
397 function PMA_DBI_field_name($result, $i)
399 return mysql_field_name($result, $i);
402 function PMA_DBI_field_flags($result, $i)
404 return mysql_field_flags($result, $i);