2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the classic MySQL extension
8 if (! defined('PHPMYADMIN')) {
15 if (! defined('PMA_MYSQL_CLIENT_API')) {
16 $client_api = explode('.', mysql_get_client_info());
17 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
21 function PMA_DBI_real_connect($server, $user, $password, $client_flags)
25 if (empty($client_flags)) {
26 if ($cfg['PersistentConnections']) {
27 $link = @mysql_pconnect
($server, $user, $password);
29 $link = @mysql_connect
($server, $user, $password);
32 if ($cfg['PersistentConnections']) {
33 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
35 $link = @mysql_connect
($server, $user, $password, false, $client_flags);
42 function PMA_DBI_connect($user, $password, $is_controluser = false)
44 global $cfg, $php_errormsg;
46 $server_port = (empty($cfg['Server']['port']))
48 : ':' . $cfg['Server']['port'];
50 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
51 $cfg['Server']['socket'] = '';
54 $server_socket = (empty($cfg['Server']['socket']))
56 : ':' . $cfg['Server']['socket'];
60 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
61 // for the case where the client library was not compiled
62 // with --enable-local-infile
65 /* Optionally compress connection */
66 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
67 $client_flags |
= MYSQL_CLIENT_COMPRESS
;
70 /* Optionally enable SSL */
71 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
72 $client_flags |
= MYSQL_CLIENT_SSL
;
75 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ?
NULL : $client_flags);
77 // Retry with empty password if we're allowed to
78 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
79 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ?
NULL : $client_flags);
83 if ($is_controluser) {
84 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING
);
90 PMA_DBI_postConnect($link, $is_controluser);
98 * @param string $dbname name of db to select
99 * @param resource $link mysql link resource
100 * @return boolean success
102 function PMA_DBI_select_db($dbname, $link = null)
105 if (isset($GLOBALS['userlink'])) {
106 $link = $GLOBALS['userlink'];
111 return mysql_select_db($dbname, $link);
115 * runs a query and returns the result
117 * @param string $query query to run
118 * @param resource $link mysql link resource
119 * @param integer $options
122 function PMA_DBI_try_query($query, $link = null, $options = 0)
125 if (isset($GLOBALS['userlink'])) {
126 $link = $GLOBALS['userlink'];
132 if ($GLOBALS['cfg']['DBG']['sql']) {
133 $time = microtime(true);
135 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
136 $r = mysql_query($query, $link);
137 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
138 $r = mysql_unbuffered_query($query, $link);
140 $r = mysql_query($query, $link);
143 if ($GLOBALS['cfg']['DBG']['sql']) {
144 $time = microtime(true) - $time;
148 if (isset($_SESSION['debug']['queries'][$hash])) {
149 $_SESSION['debug']['queries'][$hash]['count']++
;
151 $_SESSION['debug']['queries'][$hash] = array();
152 $_SESSION['debug']['queries'][$hash]['count'] = 1;
153 $_SESSION['debug']['queries'][$hash]['query'] = $query;
154 $_SESSION['debug']['queries'][$hash]['time'] = $time;
158 foreach (debug_backtrace() as $trace_step) {
159 $trace[] = PMA_Error
::relPath($trace_step['file']) . '#'
160 . $trace_step['line'] . ': '
161 . (isset($trace_step['class']) ?
$trace_step['class'] : '')
162 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
163 . (isset($trace_step['type']) ?
$trace_step['type'] : '')
164 . (isset($trace_step['function']) ?
$trace_step['function'] : '')
166 . (isset($trace_step['params']) ?
implode(', ', $trace_step['params']) : '')
170 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
176 function PMA_DBI_fetch_array($result)
178 return mysql_fetch_array($result, MYSQL_BOTH
);
181 function PMA_DBI_fetch_assoc($result) {
182 return mysql_fetch_array($result, MYSQL_ASSOC
);
185 function PMA_DBI_fetch_row($result)
187 return mysql_fetch_array($result, MYSQL_NUM
);
191 * Adjusts the result pointer to an arbitrary row in the result
193 * @uses mysql_data_seek()
196 * @return boolean true on success, false on failure
198 function PMA_DBI_data_seek($result, $offset)
200 return mysql_data_seek($result, $offset);
204 * Frees the memory associated with the results
206 * @param result $result,... one or more mysql result resources
208 function PMA_DBI_free_result()
210 foreach (func_get_args() as $result) {
211 if (is_resource($result)
212 && get_resource_type($result) === 'mysql result') {
213 mysql_free_result($result);
219 * Returns a string representing the type of connection used
220 * @uses mysql_get_host_info()
221 * @uses $GLOBALS['userlink'] as default for $link
222 * @param resource $link mysql link
223 * @return string type of connection used
225 function PMA_DBI_get_host_info($link = null)
227 if (null === $link) {
228 if (isset($GLOBALS['userlink'])) {
229 $link = $GLOBALS['userlink'];
234 return mysql_get_host_info($link);
238 * Returns the version of the MySQL protocol used
239 * @uses mysql_get_proto_info()
240 * @uses $GLOBALS['userlink'] as default for $link
241 * @param resource $link mysql link
242 * @return integer version of the MySQL protocol used
244 function PMA_DBI_get_proto_info($link = null)
246 if (null === $link) {
247 if (isset($GLOBALS['userlink'])) {
248 $link = $GLOBALS['userlink'];
253 return mysql_get_proto_info($link);
257 * returns a string that represents the client library version
258 * @uses mysql_get_client_info()
259 * @return string MySQL client library version
261 function PMA_DBI_get_client_info()
263 return mysql_get_client_info();
267 * returns last error message or false if no errors occured
269 * @uses PMA_DBI_convert_message()
270 * @uses $GLOBALS['errno']
271 * @uses $GLOBALS['userlink']
272 * @uses $GLOBALS['strServerNotResponding']
273 * @uses $GLOBALS['strSocketProblem']
274 * @uses mysql_errno()
275 * @uses mysql_error()
277 * @param resource $link mysql link
278 * @return string|boolean $error or false
280 function PMA_DBI_getError($link = null)
282 $GLOBALS['errno'] = 0;
283 if (null === $link && isset($GLOBALS['userlink'])) {
284 $link =& $GLOBALS['userlink'];
286 // Do not stop now. On the initial connection, we don't have a $link,
287 // we don't have a $GLOBALS['userlink'], but we can catch the error code
292 if (null !== $link && false !== $link) {
293 $error_number = mysql_errno($link);
294 $error_message = mysql_error($link);
296 $error_number = mysql_errno();
297 $error_message = mysql_error();
299 if (0 == $error_number) {
303 // keep the error number for further check after the call to PMA_DBI_getError()
304 $GLOBALS['errno'] = $error_number;
306 if (! empty($error_message)) {
307 $error_message = PMA_DBI_convert_message($error_message);
310 // Some errors messages cannot be obtained by mysql_error()
311 if ($error_number == 2002) {
312 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
313 } elseif ($error_number == 2003) {
314 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
316 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
321 function PMA_DBI_num_rows($result)
323 if (!is_bool($result)) {
324 return mysql_num_rows($result);
330 function PMA_DBI_insert_id($link = null)
333 if (isset($GLOBALS['userlink'])) {
334 $link = $GLOBALS['userlink'];
339 //$insert_id = mysql_insert_id($link);
340 // if the primary key is BIGINT we get an incorrect result
341 // (sometimes negative, sometimes positive)
342 // and in the present function we don't know if the PK is BIGINT
343 // so better play safe and use LAST_INSERT_ID()
345 // by the way, no problem with mysqli_insert_id()
346 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
349 function PMA_DBI_affected_rows($link = null)
352 if (isset($GLOBALS['userlink'])) {
353 $link = $GLOBALS['userlink'];
358 return mysql_affected_rows($link);
362 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
364 function PMA_DBI_get_fields_meta($result)
367 $num_fields = mysql_num_fields($result);
368 for ($i = 0; $i < $num_fields; $i++
) {
369 $fields[] = mysql_fetch_field($result, $i);
374 function PMA_DBI_num_fields($result)
376 return mysql_num_fields($result);
379 function PMA_DBI_field_len($result, $i)
381 return mysql_field_len($result, $i);
384 function PMA_DBI_field_name($result, $i)
386 return mysql_field_name($result, $i);
389 function PMA_DBI_field_flags($result, $i)
391 return mysql_field_flags($result, $i);