Fix incorrect variable name
[phpmyadmin.git] / libraries / dbi / mysql.dbi.lib.php
blobb017e103bb79781121c8b63fc8ec7c844765304c
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 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once './libraries/logging.lib.php';
14 /**
15 * MySQL client API
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])));
20 unset($client_api);
23 function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistent=false)
25 global $cfg;
27 if (empty($client_flags)) {
28 if ($cfg['PersistentConnections'] || $persistent) {
29 $link = @mysql_pconnect($server, $user, $password);
30 } else {
31 $link = @mysql_connect($server, $user, $password);
33 } else {
34 if ($cfg['PersistentConnections'] || $persistent) {
35 $link = @mysql_pconnect($server, $user, $password, $client_flags);
36 } else {
37 $link = @mysql_connect($server, $user, $password, false, $client_flags);
41 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/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;
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_persistent = (empty($server['persistent']))
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_persistent);
106 } else {
107 $link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, NULL, $server_persistent);
110 if (empty($link)) {
111 if ($is_controluser) {
112 trigger_error(__('Connection for controluser as defined in your configuration failed.'), 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_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);
164 } else {
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
186 * @param $result
187 * @param $offset
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.
220 return false;
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.
233 return false;
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'];
246 } else {
247 return false;
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'];
263 } else {
264 return false;
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) {
291 $link = null;
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
299 // } else {
300 // return false;
303 if (null !== $link && false !== $link) {
304 $error_number = mysql_errno($link);
305 $error_message = mysql_error($link);
306 } else {
307 $error_number = mysql_errno();
308 $error_message = mysql_error();
310 if (0 == $error_number) {
311 return false;
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>)';
335 } else {
336 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
338 return $error;
341 function PMA_DBI_num_rows($result)
343 if (!is_bool($result)) {
344 return mysql_num_rows($result);
345 } else {
346 return 0;
350 function PMA_DBI_insert_id($link = null)
352 if (empty($link)) {
353 if (isset($GLOBALS['userlink'])) {
354 $link = $GLOBALS['userlink'];
355 } else {
356 return false;
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)
376 if (empty($link)) {
377 if (isset($GLOBALS['userlink'])) {
378 $link = $GLOBALS['userlink'];
379 } else {
380 return false;
384 if ($get_from_cache) {
385 return $GLOBALS['cached_affected_rows'];
386 } else {
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)
396 $fields = array();
397 $num_fields = mysql_num_fields($result);
398 for ($i = 0; $i < $num_fields; $i++) {
399 $fields[] = mysql_fetch_field($result, $i);
401 return $fields;
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);