Lock page when changes are done in the SQL editor
[phpmyadmin.git] / libraries / dbi / DBIMysql.php
blob1b1236b42015aa48519a7c28b7178b0eb82fd801
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface to the classic MySQL extension
6 * @package PhpMyAdmin-DBI
7 * @subpackage MySQL
8 */
9 namespace PMA\libraries\dbi;
11 use PMA\libraries\DatabaseInterface;
13 if (! defined('PHPMYADMIN')) {
14 exit;
17 if (! extension_loaded('mysql')) {
18 // The old MySQL extension is deprecated as of PHP 5.5.0, and will be
19 // removed in the future. Instead, the `MySQLi` or `PDO_MySQL` extension
20 // should be used.
21 return;
24 require_once 'libraries/dbi/DBIExtension.lib.php';
26 /**
27 * MySQL client API
29 PMA_defineClientAPI(mysql_get_client_info());
31 /**
32 * Interface to the classic MySQL extension
34 * @package PhpMyAdmin-DBI
35 * @subpackage MySQL
37 class DBIMysql implements DBIExtension
39 /**
40 * Helper function for connecting to the database server
42 * @param string $server host/port/socket
43 * @param string $user mysql user name
44 * @param string $password mysql user password
45 * @param int $client_flags client flags of connection
46 * @param bool $persistent whether to use persistent connection
48 * @return mixed false on error or a mysql connection resource on success
50 private function _realConnect($server, $user, $password, $client_flags,
51 $persistent = false
52 ) {
53 global $cfg;
55 if (empty($client_flags)) {
56 if ($cfg['PersistentConnections'] || $persistent) {
57 $link = @mysql_pconnect($server, $user, $password);
58 } else {
59 $link = @mysql_connect($server, $user, $password);
61 } else {
62 if ($cfg['PersistentConnections'] || $persistent) {
63 $link = @mysql_pconnect($server, $user, $password, $client_flags);
64 } else {
65 $link = @mysql_connect(
66 $server, $user, $password, false, $client_flags
71 return $link;
74 /**
75 * Run the multi query and output the results
77 * @param mysqli $link mysqli object
78 * @param string $query multi query statement to execute
80 * @return boolean false always false since mysql extension not support
81 * for multi query executions
83 public function realMultiQuery($link, $query)
85 // N.B.: PHP's 'mysql' extension does not support
86 // multi_queries so this function will always
87 // return false. Use the 'mysqli' extension, if
88 // you need support for multi_queries.
89 return false;
92 /**
93 * connects to the database server
95 * @param string $user mysql user name
96 * @param string $password mysql user password
97 * @param array $server host/port/socket/persistent
99 * @return mixed false on error or a mysqli object on success
101 public function connect(
102 $user, $password, $server
104 if ($server['port'] === 0) {
105 $server_port = '';
106 } else {
107 $server_port = ':' . $server['port'];
110 if (is_null($server['socket'])) {
111 $server_socket = '';
112 } else {
113 $server_socket = ':' . $server['socket'];
116 $client_flags = 0;
118 if (defined('PMA_ENABLE_LDI')) {
119 // use CLIENT_LOCAL_FILES as defined in mysql_com.h
120 // for the case where the client library was not compiled
121 // with --enable-local-infile
122 $client_flags |= 128;
125 /* Optionally compress connection */
126 if (defined('MYSQL_CLIENT_COMPRESS') && $server['compress']) {
127 $client_flags |= MYSQL_CLIENT_COMPRESS;
130 /* Optionally enable SSL */
131 if (defined('MYSQL_CLIENT_SSL') && $server['ssl']) {
132 $client_flags |= MYSQL_CLIENT_SSL;
135 if (!isset($server['host'])) {
136 $link = $this->_realConnect($server_socket, $user, $password, null);
137 } else {
138 $link = $this->_realConnect(
139 $server['host'] . $server_port . $server_socket,
140 $user, $password, null
143 return $link;
147 * selects given database
149 * @param string $dbname name of db to select
150 * @param resource|null $link mysql link resource
152 * @return bool
154 public function selectDb($dbname, $link)
156 return mysql_select_db($dbname, $link);
160 * runs a query and returns the result
162 * @param string $query query to run
163 * @param resource|null $link mysql link resource
164 * @param int $options query options
166 * @return mixed
168 public function realQuery($query, $link, $options)
170 if ($options == ($options | DatabaseInterface::QUERY_STORE)) {
171 return mysql_query($query, $link);
172 } elseif ($options == ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
173 return mysql_unbuffered_query($query, $link);
174 } else {
175 return mysql_query($query, $link);
180 * returns array of rows with associative and numeric keys from $result
182 * @param resource $result result MySQL result
184 * @return array
186 public function fetchArray($result)
188 return mysql_fetch_array($result, MYSQL_BOTH);
192 * returns array of rows with associative keys from $result
194 * @param resource $result MySQL result
196 * @return array
198 public function fetchAssoc($result)
200 return mysql_fetch_array($result, MYSQL_ASSOC);
204 * returns array of rows with numeric keys from $result
206 * @param resource $result MySQL result
208 * @return array
210 public function fetchRow($result)
212 return mysql_fetch_array($result, MYSQL_NUM);
216 * Adjusts the result pointer to an arbitrary row in the result
218 * @param resource $result database result
219 * @param integer $offset offset to seek
221 * @return bool true on success, false on failure
223 public function dataSeek($result, $offset)
225 return mysql_data_seek($result, $offset);
229 * Frees memory associated with the result
231 * @param resource $result database result
233 * @return void
235 public function freeResult($result)
237 if (is_resource($result) && get_resource_type($result) === 'mysql result') {
238 mysql_free_result($result);
243 * Check if there are any more query results from a multi query
245 * @param resource $link the connection object
247 * @return bool false
249 public function moreResults($link)
251 // N.B.: PHP's 'mysql' extension does not support
252 // multi_queries so this function will always
253 // return false. Use the 'mysqli' extension, if
254 // you need support for multi_queries.
255 return false;
259 * Prepare next result from multi_query
261 * @param resource $link the connection object
263 * @return boolean false
265 public function nextResult($link)
267 // N.B.: PHP's 'mysql' extension does not support
268 // multi_queries so this function will always
269 // return false. Use the 'mysqli' extension, if
270 // you need support for multi_queries.
271 return false;
275 * Returns a string representing the type of connection used
277 * @param resource|null $link mysql link
279 * @return string type of connection used
281 public function getHostInfo($link)
283 return mysql_get_host_info($link);
287 * Returns the version of the MySQL protocol used
289 * @param resource|null $link mysql link
291 * @return int version of the MySQL protocol used
293 public function getProtoInfo($link)
295 return mysql_get_proto_info($link);
299 * returns a string that represents the client library version
301 * @return string MySQL client library version
303 public function getClientInfo()
305 return mysql_get_client_info();
309 * returns last error message or false if no errors occurred
311 * @param resource|null $link mysql link
313 * @return string|bool $error or false
315 public function getError($link)
317 $GLOBALS['errno'] = 0;
319 if (null !== $link && false !== $link) {
320 $error_number = mysql_errno($link);
321 $error_message = mysql_error($link);
322 } else {
323 $error_number = mysql_errno();
324 $error_message = mysql_error();
326 if (0 == $error_number) {
327 return false;
330 // keep the error number for further check after
331 // the call to getError()
332 $GLOBALS['errno'] = $error_number;
334 return $GLOBALS['dbi']->formatError($error_number, $error_message);
338 * returns the number of rows returned by last query
340 * @param resource $result MySQL result
342 * @return string|int
344 public function numRows($result)
346 if (is_bool($result)) {
347 return 0;
350 return mysql_num_rows($result);
354 * returns the number of rows affected by last query
356 * @param resource|null $link the mysql object
358 * @return int
360 public function affectedRows($link)
362 return mysql_affected_rows($link);
366 * returns metainfo for fields in $result
368 * @param resource $result MySQL result
370 * @return array meta info for fields in $result
372 * @todo add missing keys like in mysqli_query (decimals)
374 public function getFieldsMeta($result)
376 $fields = array();
377 $num_fields = mysql_num_fields($result);
378 for ($i = 0; $i < $num_fields; $i++) {
379 $field = mysql_fetch_field($result, $i);
380 $field->flags = mysql_field_flags($result, $i);
381 $field->orgtable = mysql_field_table($result, $i);
382 $field->orgname = mysql_field_name($result, $i);
383 $fields[] = $field;
385 return $fields;
389 * return number of fields in given $result
391 * @param resource $result MySQL result
393 * @return int field count
395 public function numFields($result)
397 return mysql_num_fields($result);
401 * returns the length of the given field $i in $result
403 * @param resource $result MySQL result
404 * @param int $i field
406 * @return int length of field
408 public function fieldLen($result, $i)
410 return mysql_field_len($result, $i);
414 * returns name of $i. field in $result
416 * @param resource $result MySQL result
417 * @param int $i field
419 * @return string name of $i. field in $result
421 public function fieldName($result, $i)
423 return mysql_field_name($result, $i);
427 * returns concatenated string of human readable field flags
429 * @param resource $result MySQL result
430 * @param int $i field
432 * @return string field flags
434 public function fieldFlags($result, $i)
436 return mysql_field_flags($result, $i);
440 * Store the result returned from multi query
442 * @param resource $result MySQL result
444 * @return false
446 public function storeResult($result)
448 return false;
452 * returns properly escaped string for use in MySQL queries
454 * @param mixed $link database link
455 * @param string $str string to be escaped
457 * @return string a MySQL escaped string
459 public function escapeString($link, $str)
461 return mysql_real_escape_string($str, $link);