sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / dbi / mysql.dbi.lib.php
blobc88f624692460eb5872a7152238fc1591f9de984
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Interface to the classic MySQL extension
7 */
9 // MySQL client API
10 if (!defined('PMA_MYSQL_CLIENT_API')) {
11 if (function_exists('mysql_get_client_info')) {
12 $client_api = explode('.', mysql_get_client_info());
13 define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
14 unset($client_api);
15 } else {
16 define('PMA_MYSQL_CLIENT_API', 32332); // always expect the worst...
20 function PMA_DBI_connect($user, $password, $is_controluser = FALSE) {
21 global $cfg, $php_errormsg;
23 $server_port = (empty($cfg['Server']['port']))
24 ? ''
25 : ':' . $cfg['Server']['port'];
27 if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
28 $cfg['Server']['socket'] = '';
31 $server_socket = (empty($cfg['Server']['socket']))
32 ? ''
33 : ':' . $cfg['Server']['socket'];
35 if (PMA_PHP_INT_VERSION >= 40300 && PMA_MYSQL_CLIENT_API >= 32349) {
36 $client_flags = $cfg['Server']['compress'] && defined('MYSQL_CLIENT_COMPRESS') ? MYSQL_CLIENT_COMPRESS : 0;
37 // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
38 // for the case where the client library was not compiled
39 // with --enable-local-infile
40 $client_flags |= 128;
43 if (empty($client_flags)) {
44 $connect_func = 'mysql_' . ($cfg['PersistentConnections'] ? 'p' : '') . 'connect';
45 $link = @$connect_func($cfg['Server']['host'] . $server_port . $server_socket, $user, $password);
46 } else {
47 if ($cfg['PersistentConnections']) {
48 $link = @mysql_pconnect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, $client_flags);
49 } else {
50 $link = @mysql_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, FALSE, $client_flags);
54 if (empty($link)) {
55 PMA_auth_fails();
56 } // end if
58 PMA_DBI_postConnect($link, $is_controluser);
60 return $link;
63 function PMA_DBI_select_db($dbname, $link = NULL) {
64 if (empty($link)) {
65 if (isset($GLOBALS['userlink'])) {
66 $link = $GLOBALS['userlink'];
67 } else {
68 return FALSE;
71 if (PMA_MYSQL_INT_VERSION < 40100) {
72 $dbname = PMA_convert_charset($dbname);
74 return mysql_select_db($dbname, $link);
77 function PMA_DBI_try_query($query, $link = NULL, $options = 0) {
78 if (empty($link)) {
79 if (isset($GLOBALS['userlink'])) {
80 $link = $GLOBALS['userlink'];
81 } else {
82 return FALSE;
85 if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) {
86 $query = PMA_convert_charset($query);
88 if ($options == ($options | PMA_DBI_QUERY_STORE)) {
89 return @mysql_query($query, $link);
90 } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
91 return @mysql_unbuffered_query($query, $link);
92 } else {
93 return @mysql_query($query, $link);
97 // The following function is meant for internal use only.
98 // Do not call it from outside this library!
99 function PMA_mysql_fetch_array($result, $type = FALSE) {
100 global $cfg, $allow_recoding, $charset, $convcharset;
102 if ($type != FALSE) {
103 $data = mysql_fetch_array($result, $type);
104 } else {
105 $data = mysql_fetch_array($result);
108 /* No data returned => do not touch it */
109 if (! $data) return $data;
111 if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100
112 || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
113 /* No recoding -> return data as we got them */
114 return $data;
115 } else {
116 $ret = array();
117 $num = mysql_num_fields($result);
118 $i = 0;
119 for ($i = 0; $i < $num; $i++) {
120 $name = mysql_field_name($result, $i);
121 $flags = mysql_field_flags($result, $i);
122 /* Field is BINARY (either marked manually, or it is BLOB) => do not convert it */
123 if (stristr($flags, 'BINARY')) {
124 if (isset($data[$i])) $ret[$i] = $data[$i];
125 if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = $data[$name];
126 } else {
127 if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
128 if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
131 return $ret;
135 function PMA_DBI_fetch_array($result) {
136 return PMA_mysql_fetch_array($result);
139 function PMA_DBI_fetch_assoc($result) {
140 return PMA_mysql_fetch_array($result, MYSQL_ASSOC);
143 function PMA_DBI_fetch_row($result) {
144 return PMA_mysql_fetch_array($result, MYSQL_NUM);
147 function PMA_DBI_free_result($result) {
148 if (!is_bool($result)) {
149 return mysql_free_result($result);
150 } else {
151 return 0;
156 * returns last error message or false if no errors occured
158 * @uses PMA_MYSQL_INT_VERSION
159 * @uses PMA_convert_display_charset()
160 * @uses PMA_DBI_convert_message()
161 * @uses $GLOBALS['errno']
162 * @uses $GLOBALS['userlink']
163 * @uses $GLOBALS['strServerNotResponding']
164 * @uses $GLOBALS['strSocketProblem']
165 * @uses mysql_errno()
166 * @uses mysql_error()
167 * @uses defined()
168 * @param resource $link mysql link
169 * @return string|boolean $error or false
171 function PMA_DBI_getError( $link = NULL ) {
172 unset( $GLOBALS['errno'] );
173 if ( NULL === $link && isset( $GLOBALS['userlink'] ) ) {
174 $link =& $GLOBALS['userlink'];
176 // Do not stop now. On the initial connection, we don't have a $link,
177 // we don't have a $GLOBALS['userlink'], but we can catch the error code
178 // } else {
179 // return FALSE;
182 if ( NULL !== $link ) {
183 $error_number = mysql_errno( $link );
184 $error_message = mysql_error( $link );
185 } else {
186 $error_number = mysql_errno();
187 $error_message = mysql_error();
189 if ( 0 == $error_number ) {
190 return false;
193 // keep the error number for further check after the call to PMA_DBI_getError()
194 $GLOBALS['errno'] = $error_number;
196 if ( ! empty( $error_message ) ) {
197 $error_message = PMA_DBI_convert_message( $error_message );
200 // Some errors messages cannot be obtained by mysql_error()
201 if ( $error_number == 2002 ) {
202 $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
203 } elseif ( $error_number == 2003 ) {
204 $error = '#' . ((string) $error_number ) . ' - ' . $GLOBALS['strServerNotResponding'];
205 } elseif ( defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100 ) {
206 $error = '#' . ((string) $error_number ) . ' - ' . $error_message;
207 } else {
208 $error = '#' . ((string) $error_number ) . ' - ' . PMA_convert_display_charset( $error_message );
210 return $error;
213 function PMA_DBI_close($link = NULL) {
214 if (empty($link)) {
215 if (isset($GLOBALS['userlink'])) {
216 $link = $GLOBALS['userlink'];
217 } else {
218 return FALSE;
221 return @mysql_close($link);
224 function PMA_DBI_num_rows($result) {
225 if (!is_bool($result)) {
226 return mysql_num_rows($result);
227 } else {
228 return 0;
232 function PMA_DBI_insert_id($link = NULL) {
233 if (empty($link)) {
234 if (isset($GLOBALS['userlink'])) {
235 $link = $GLOBALS['userlink'];
236 } else {
237 return FALSE;
240 return mysql_insert_id($link);
243 function PMA_DBI_affected_rows($link = NULL) {
244 if (empty($link)) {
245 if (isset($GLOBALS['userlink'])) {
246 $link = $GLOBALS['userlink'];
247 } else {
248 return FALSE;
251 return mysql_affected_rows($link);
254 function PMA_DBI_get_fields_meta($result) {
255 $fields = array();
256 $num_fields = mysql_num_fields($result);
257 for ($i = 0; $i < $num_fields; $i++) {
258 $fields[] = PMA_convert_display_charset(mysql_fetch_field($result, $i));
260 return $fields;
263 function PMA_DBI_num_fields($result) {
264 return mysql_num_fields($result);
267 function PMA_DBI_field_len($result, $i) {
268 return mysql_field_len($result, $i);
271 function PMA_DBI_field_name($result, $i) {
272 return mysql_field_name($result, $i);
275 function PMA_DBI_field_flags($result, $i) {
276 return PMA_convert_display_charset(mysql_field_flags($result, $i));