2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Interface to the classic MySQL extension
8 if (! defined('PHPMYADMIN')) {
16 if (!defined('PMA_MYSQL_CLIENT_API')) {
17 if (function_exists('mysql_get_client_info')) {
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])));
22 define('PMA_MYSQL_CLIENT_API', 32332); // always expect the worst...
26 function PMA_DBI_real_connect($server, $user, $password, $client_flags) {
29 if (empty($client_flags)) {
30 if ($cfg['PersistentConnections']) {
31 $link = @mysql_pconnect
($server, $user, $password);
33 $link = @mysql_connect
($server, $user, $password);
36 if ($cfg['PersistentConnections']) {
37 $link = @mysql_pconnect
($server, $user, $password, $client_flags);
39 $link = @mysql_connect
($server, $user, $password, FALSE, $client_flags);
46 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 if (PMA_PHP_INT_VERSION
>= 40300 && PMA_MYSQL_CLIENT_API
>= 32349) {
64 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
65 // for the case where the client library was not compiled
66 // with --enable-local-infile
70 /* Optionally compress connection */
71 if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
72 $client_flags |
= MYSQL_CLIENT_COMPRESS
;
75 /* Optionally enable SSL */
76 if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
77 $client_flags |
= MYSQL_CLIENT_SSL
;
80 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ?
NULL : $client_flags);
82 // Retry with empty password if we're allowed to
83 if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
84 $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ?
NULL : $client_flags);
88 if ($is_controluser) {
89 if (! defined('PMA_DBI_CONNECT_FAILED_CONTROLUSER')) {
90 define('PMA_DBI_CONNECT_FAILED_CONTROLUSER', true);
97 PMA_DBI_postConnect($link, $is_controluser);
102 function PMA_DBI_select_db($dbname, $link = null) {
104 if (isset($GLOBALS['userlink'])) {
105 $link = $GLOBALS['userlink'];
110 if (PMA_MYSQL_INT_VERSION
< 40100) {
111 $dbname = PMA_convert_charset($dbname);
113 return mysql_select_db($dbname, $link);
116 function PMA_DBI_try_query($query, $link = null, $options = 0) {
118 if (isset($GLOBALS['userlink'])) {
119 $link = $GLOBALS['userlink'];
124 if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION
< 40100) {
125 $query = PMA_convert_charset($query);
127 if ($options == ($options | PMA_DBI_QUERY_STORE
)) {
128 return @mysql_query
($query, $link);
129 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED
)) {
130 return @mysql_unbuffered_query
($query, $link);
132 return @mysql_query
($query, $link);
136 // The following function is meant for internal use only.
137 // Do not call it from outside this library!
138 function PMA_mysql_fetch_array($result, $type = FALSE) {
139 global $cfg, $allow_recoding, $charset, $convcharset;
141 if ($type != FALSE) {
142 $data = mysql_fetch_array($result, $type);
144 $data = mysql_fetch_array($result);
147 /* No data returned => do not touch it */
152 if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION
>= 40100
153 ||
!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
154 /* No recoding -> return data as we got them */
158 $num = mysql_num_fields($result);
160 for ($i = 0; $i < $num; $i++
) {
161 $name = mysql_field_name($result, $i);
162 $flags = mysql_field_flags($result, $i);
163 /* Field is BINARY (either marked manually, or it is BLOB) => do not convert it */
164 if (stristr($flags, 'BINARY')) {
165 if (isset($data[$i])) {
166 $ret[$i] = $data[$i];
168 if (isset($data[$name])) {
169 $ret[PMA_convert_display_charset($name)] = $data[$name];
172 if (isset($data[$i])) {
173 $ret[$i] = PMA_convert_display_charset($data[$i]);
175 if (isset($data[$name])) {
176 $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
184 function PMA_DBI_fetch_array($result) {
185 return PMA_mysql_fetch_array($result);
188 function PMA_DBI_fetch_assoc($result) {
189 return PMA_mysql_fetch_array($result, MYSQL_ASSOC
);
192 function PMA_DBI_fetch_row($result) {
193 return PMA_mysql_fetch_array($result, MYSQL_NUM
);
197 * Frees the memory associated with the results
199 * @param result $result,... one or more mysql result resources
201 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 * Returns a string representing the type of connection used
212 * @uses mysql_get_host_info()
213 * @uses $GLOBALS['userlink'] as default for $link
214 * @param resource $link mysql link
215 * @return string type of connection used
217 function PMA_DBI_get_host_info($link = null)
219 if (null === $link) {
220 if (isset($GLOBALS['userlink'])) {
221 $link = $GLOBALS['userlink'];
226 return mysql_get_host_info($link);
230 * Returns the version of the MySQL protocol used
231 * @uses mysql_get_proto_info()
232 * @uses $GLOBALS['userlink'] as default for $link
233 * @param resource $link mysql link
234 * @return integer version of the MySQL protocol used
236 function PMA_DBI_get_proto_info($link = null)
238 if (null === $link) {
239 if (isset($GLOBALS['userlink'])) {
240 $link = $GLOBALS['userlink'];
245 return mysql_get_proto_info($link);
249 * returns a string that represents the client library version
250 * @uses mysql_get_client_info()
251 * @return string MySQL client library version
253 function PMA_DBI_get_client_info() {
254 return mysql_get_client_info();
258 * returns last error message or false if no errors occured
260 * @uses PMA_MYSQL_INT_VERSION
261 * @uses PMA_convert_display_charset()
262 * @uses PMA_DBI_convert_message()
263 * @uses $GLOBALS['errno']
264 * @uses $GLOBALS['userlink']
265 * @uses $GLOBALS['strServerNotResponding']
266 * @uses $GLOBALS['strSocketProblem']
267 * @uses mysql_errno()
268 * @uses mysql_error()
270 * @param resource $link mysql link
271 * @return string|boolean $error or false
273 function PMA_DBI_getError($link = null)
275 $GLOBALS['errno'] = 0;
276 if (null === $link && isset($GLOBALS['userlink'])) {
277 $link =& $GLOBALS['userlink'];
279 // Do not stop now. On the initial connection, we don't have a $link,
280 // we don't have a $GLOBALS['userlink'], but we can catch the error code
285 if (null !== $link) {
286 $error_number = mysql_errno($link);
287 $error_message = mysql_error($link);
289 $error_number = mysql_errno();
290 $error_message = mysql_error();
292 if (0 == $error_number) {
296 // keep the error number for further check after the call to PMA_DBI_getError()
297 $GLOBALS['errno'] = $error_number;
299 if (! empty($error_message)) {
300 $error_message = PMA_DBI_convert_message($error_message);
303 // Some errors messages cannot be obtained by mysql_error()
304 if ($error_number == 2002) {
305 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
306 } elseif ($error_number == 2003) {
307 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
308 } elseif (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION
>= 40100) {
309 $error = '#' . ((string) $error_number) . ' - ' . $error_message;
311 $error = '#' . ((string) $error_number) . ' - ' . PMA_convert_display_charset($error_message);
316 function PMA_DBI_close($link = null)
319 if (isset($GLOBALS['userlink'])) {
320 $link = $GLOBALS['userlink'];
325 return @mysql_close
($link);
328 function PMA_DBI_num_rows($result) {
329 if (!is_bool($result)) {
330 return mysql_num_rows($result);
336 function PMA_DBI_insert_id($link = null)
339 if (isset($GLOBALS['userlink'])) {
340 $link = $GLOBALS['userlink'];
345 //$insert_id = mysql_insert_id($link);
346 // if the primary key is BIGINT we get an incorrect result
347 // (sometimes negative, sometimes positive)
348 // and in the present function we don't know if the PK is BIGINT
349 // so better play safe and use LAST_INSERT_ID()
351 // by the way, no problem with mysqli_insert_id()
352 return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
355 function PMA_DBI_affected_rows($link = null)
358 if (isset($GLOBALS['userlink'])) {
359 $link = $GLOBALS['userlink'];
364 return mysql_affected_rows($link);
368 * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
370 function PMA_DBI_get_fields_meta($result) {
372 $num_fields = mysql_num_fields($result);
373 for ($i = 0; $i < $num_fields; $i++
) {
374 $fields[] = PMA_convert_display_charset(mysql_fetch_field($result, $i));
379 function PMA_DBI_num_fields($result) {
380 return mysql_num_fields($result);
383 function PMA_DBI_field_len($result, $i) {
384 return mysql_field_len($result, $i);
387 function PMA_DBI_field_name($result, $i) {
388 return mysql_field_name($result, $i);
391 function PMA_DBI_field_flags($result, $i) {
392 return PMA_convert_display_charset(mysql_field_flags($result, $i));