remove todo, after upgrade PHPExcel
[phpmyadmin/arisferyanto.git] / libraries / dbi / mysql.dbi.lib.php
blob2754588328df01103bc499db8e6cd1dec26a065d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface to the classic MySQL extension
6 * @package phpMyAdmin-DBI-MySQL
7 * @version $Id$
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 require_once './libraries/logging.lib.php';
15 /**
16 * MySQL client API
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])));
21 unset($client_api);
24 function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistant=false)
26 global $cfg;
28 if (empty($client_flags)) {
29 if ($cfg['PersistentConnections'] || $persistant) {
30 $link = @mysql_pconnect($server, $user, $password);
31 } else {
32 $link = @mysql_connect($server, $user, $password);
34 } else {
35 if ($cfg['PersistentConnections'] || $persistant) {
36 $link = @mysql_pconnect($server, $user, $password, $client_flags);
37 } else {
38 $link = @mysql_connect($server, $user, $password, false, $client_flags);
42 return $link;
44 /**
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;
56 if ($server) {
57 $server_port = (empty($server['port']))
58 ? ''
59 : ':' . (int)$server['port'];
60 $server_socket = (empty($server['socket']))
61 ? ''
62 : ':' . $server['socket'];
63 $server_persistant = (empty($server['persistant']))
64 ? false
65 : true;
66 } else {
67 $server_port = (empty($cfg['Server']['port']))
68 ? ''
69 : ':' . (int)$cfg['Server']['port'];
70 $server_socket = (empty($cfg['Server']['socket']))
71 ? ''
72 : ':' . $cfg['Server']['socket'];
75 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
76 $cfg['Server']['socket'] = '';
79 $client_flags = 0;
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
84 $client_flags |= 128;
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;
96 if (!$server) {
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);
103 } else {
104 if (!isset($server['host'])) {
105 $link = PMA_DBI_real_connect($server_socket, $user, $password, NULL, $server_persistant);
106 } else {
107 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, NULL, $server_persistant);
110 if (empty($link)) {
111 if ($is_controluser) {
112 trigger_error($GLOBALS['strControluserFailed'], E_USER_WARNING);
113 return false;
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');
120 PMA_auth_fails();
121 } else {
122 return false;
124 } // end if
125 if (! $server) {
126 PMA_DBI_postConnect($link, $is_controluser);
128 return $link;
132 * select a db
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)
140 if (empty($link)) {
141 if (isset($GLOBALS['userlink'])) {
142 $link = $GLOBALS['userlink'];
143 } else {
144 return false;
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
156 * @return mixed
158 function PMA_DBI_try_query($query, $link = null, $options = 0)
160 if (empty($link)) {
161 if (isset($GLOBALS['userlink'])) {
162 $link = $GLOBALS['userlink'];
163 } else {
164 return false;
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);
175 } else {
176 $r = mysql_query($query, $link);
179 if ($GLOBALS['cfg']['DBG']['sql']) {
180 $time = microtime(true) - $time;
182 $hash = md5($query);
184 if (isset($_SESSION['debug']['queries'][$hash])) {
185 $_SESSION['debug']['queries'][$hash]['count']++;
186 } else {
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;
193 $trace = array();
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'] : '')
201 . '('
202 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
203 . ')'
206 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
208 if ($r != FALSE && PMA_Tracker::isActive() == TRUE ) {
209 PMA_Tracker::handleQuery($query);
212 return $r;
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()
233 * @param $result
234 * @param $offset
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'];
269 } else {
270 return false;
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'];
288 } else {
289 return false;
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()
316 * @uses defined()
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
329 // } else {
330 // return false;
333 if (null !== $link && false !== $link) {
334 $error_number = mysql_errno($link);
335 $error_message = mysql_error($link);
336 } else {
337 $error_number = mysql_errno();
338 $error_message = mysql_error();
340 if (0 == $error_number) {
341 return false;
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>)';
363 } else {
364 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
366 return $error;
369 function PMA_DBI_num_rows($result)
371 if (!is_bool($result)) {
372 return mysql_num_rows($result);
373 } else {
374 return 0;
378 function PMA_DBI_insert_id($link = null)
380 if (empty($link)) {
381 if (isset($GLOBALS['userlink'])) {
382 $link = $GLOBALS['userlink'];
383 } else {
384 return false;
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)
399 if (empty($link)) {
400 if (isset($GLOBALS['userlink'])) {
401 $link = $GLOBALS['userlink'];
402 } else {
403 return false;
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)
414 $fields = array();
415 $num_fields = mysql_num_fields($result);
416 for ($i = 0; $i < $num_fields; $i++) {
417 $fields[] = mysql_fetch_field($result, $i);
419 return $fields;
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);