DbiMysql.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Interface to the classic MySQL extension
  5. *
  6. * @package PhpMyAdmin-DBI
  7. * @subpackage MySQL
  8. */
  9. namespace PhpMyAdmin\Dbi;
  10. use PhpMyAdmin\DatabaseInterface;
  11. if (! defined('PHPMYADMIN')) {
  12. exit;
  13. }
  14. if (! extension_loaded('mysql')) {
  15. // The old MySQL extension is deprecated as of PHP 5.5.0, and will be
  16. // removed in the future. Instead, the `MySQLi` or `PDO_MySQL` extension
  17. // should be used.
  18. return;
  19. }
  20. /**
  21. * Interface to the classic MySQL extension
  22. *
  23. * @package PhpMyAdmin-DBI
  24. * @subpackage MySQL
  25. */
  26. class DbiMysql implements DbiExtension
  27. {
  28. /**
  29. * Helper function for connecting to the database server
  30. *
  31. * @param string $server host/port/socket
  32. * @param string $user mysql user name
  33. * @param string $password mysql user password
  34. * @param int $client_flags client flags of connection
  35. * @param bool $persistent whether to use persistent connection
  36. *
  37. * @return mixed false on error or a mysql connection resource on success
  38. */
  39. private function _realConnect($server, $user, $password, $client_flags,
  40. $persistent = false
  41. ) {
  42. global $cfg;
  43. if (empty($client_flags)) {
  44. if ($cfg['PersistentConnections'] || $persistent) {
  45. $link = @mysql_pconnect($server, $user, $password);
  46. } else {
  47. $link = @mysql_connect($server, $user, $password);
  48. }
  49. } else {
  50. if ($cfg['PersistentConnections'] || $persistent) {
  51. $link = @mysql_pconnect($server, $user, $password, $client_flags);
  52. } else {
  53. $link = @mysql_connect(
  54. $server, $user, $password, false, $client_flags
  55. );
  56. }
  57. }
  58. return $link;
  59. }
  60. /**
  61. * Run the multi query and output the results
  62. *
  63. * @param mysqli $link mysqli object
  64. * @param string $query multi query statement to execute
  65. *
  66. * @return boolean false always false since mysql extension not support
  67. * for multi query executions
  68. */
  69. public function realMultiQuery($link, $query)
  70. {
  71. // N.B.: PHP's 'mysql' extension does not support
  72. // multi_queries so this function will always
  73. // return false. Use the 'mysqli' extension, if
  74. // you need support for multi_queries.
  75. return false;
  76. }
  77. /**
  78. * connects to the database server
  79. *
  80. * @param string $user mysql user name
  81. * @param string $password mysql user password
  82. * @param array $server host/port/socket/persistent
  83. *
  84. * @return mixed false on error or a mysqli object on success
  85. */
  86. public function connect(
  87. $user, $password, array $server
  88. ) {
  89. if ($server['port'] === 0) {
  90. $server_port = '';
  91. } else {
  92. $server_port = ':' . $server['port'];
  93. }
  94. if (is_null($server['socket'])) {
  95. $server_socket = '';
  96. } else {
  97. $server_socket = ':' . $server['socket'];
  98. }
  99. $client_flags = 0;
  100. if (defined('PMA_ENABLE_LDI')) {
  101. // use CLIENT_LOCAL_FILES as defined in mysql_com.h
  102. // for the case where the client library was not compiled
  103. // with --enable-local-infile
  104. $client_flags |= 128;
  105. }
  106. /* Optionally compress connection */
  107. if (defined('MYSQL_CLIENT_COMPRESS') && $server['compress']) {
  108. $client_flags |= MYSQL_CLIENT_COMPRESS;
  109. }
  110. /* Optionally enable SSL */
  111. if (defined('MYSQL_CLIENT_SSL') && $server['ssl']) {
  112. $client_flags |= MYSQL_CLIENT_SSL;
  113. }
  114. if (!isset($server['host'])) {
  115. $link = $this->_realConnect($server_socket, $user, $password, null);
  116. } else {
  117. $link = $this->_realConnect(
  118. $server['host'] . $server_port . $server_socket,
  119. $user, $password, null
  120. );
  121. }
  122. return $link;
  123. }
  124. /**
  125. * selects given database
  126. *
  127. * @param string $dbname name of db to select
  128. * @param resource|null $link mysql link resource
  129. *
  130. * @return bool
  131. */
  132. public function selectDb($dbname, $link)
  133. {
  134. return mysql_select_db($dbname, $link);
  135. }
  136. /**
  137. * runs a query and returns the result
  138. *
  139. * @param string $query query to run
  140. * @param resource|null $link mysql link resource
  141. * @param int $options query options
  142. *
  143. * @return mixed
  144. */
  145. public function realQuery($query, $link, $options)
  146. {
  147. if ($options == ($options | DatabaseInterface::QUERY_STORE)) {
  148. return mysql_query($query, $link);
  149. } elseif ($options == ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
  150. return mysql_unbuffered_query($query, $link);
  151. }
  152. return mysql_query($query, $link);
  153. }
  154. /**
  155. * returns array of rows with associative and numeric keys from $result
  156. *
  157. * @param resource $result result MySQL result
  158. *
  159. * @return array
  160. */
  161. public function fetchArray($result)
  162. {
  163. return mysql_fetch_array($result, MYSQL_BOTH);
  164. }
  165. /**
  166. * returns array of rows with associative keys from $result
  167. *
  168. * @param resource $result MySQL result
  169. *
  170. * @return array
  171. */
  172. public function fetchAssoc($result)
  173. {
  174. return mysql_fetch_array($result, MYSQL_ASSOC);
  175. }
  176. /**
  177. * returns array of rows with numeric keys from $result
  178. *
  179. * @param resource $result MySQL result
  180. *
  181. * @return array
  182. */
  183. public function fetchRow($result)
  184. {
  185. return mysql_fetch_array($result, MYSQL_NUM);
  186. }
  187. /**
  188. * Adjusts the result pointer to an arbitrary row in the result
  189. *
  190. * @param resource $result database result
  191. * @param integer $offset offset to seek
  192. *
  193. * @return bool true on success, false on failure
  194. */
  195. public function dataSeek($result, $offset)
  196. {
  197. return mysql_data_seek($result, $offset);
  198. }
  199. /**
  200. * Frees memory associated with the result
  201. *
  202. * @param resource $result database result
  203. *
  204. * @return void
  205. */
  206. public function freeResult($result)
  207. {
  208. if (is_resource($result) && get_resource_type($result) === 'mysql result') {
  209. mysql_free_result($result);
  210. }
  211. }
  212. /**
  213. * Check if there are any more query results from a multi query
  214. *
  215. * @param resource $link the connection object
  216. *
  217. * @return bool false
  218. */
  219. public function moreResults($link)
  220. {
  221. // N.B.: PHP's 'mysql' extension does not support
  222. // multi_queries so this function will always
  223. // return false. Use the 'mysqli' extension, if
  224. // you need support for multi_queries.
  225. return false;
  226. }
  227. /**
  228. * Prepare next result from multi_query
  229. *
  230. * @param resource $link the connection object
  231. *
  232. * @return boolean false
  233. */
  234. public function nextResult($link)
  235. {
  236. // N.B.: PHP's 'mysql' extension does not support
  237. // multi_queries so this function will always
  238. // return false. Use the 'mysqli' extension, if
  239. // you need support for multi_queries.
  240. return false;
  241. }
  242. /**
  243. * Returns a string representing the type of connection used
  244. *
  245. * @param resource|null $link mysql link
  246. *
  247. * @return string type of connection used
  248. */
  249. public function getHostInfo($link)
  250. {
  251. return mysql_get_host_info($link);
  252. }
  253. /**
  254. * Returns the version of the MySQL protocol used
  255. *
  256. * @param resource|null $link mysql link
  257. *
  258. * @return int version of the MySQL protocol used
  259. */
  260. public function getProtoInfo($link)
  261. {
  262. return mysql_get_proto_info($link);
  263. }
  264. /**
  265. * returns a string that represents the client library version
  266. *
  267. * @return string MySQL client library version
  268. */
  269. public function getClientInfo()
  270. {
  271. return mysql_get_client_info();
  272. }
  273. /**
  274. * returns last error message or false if no errors occurred
  275. *
  276. * @param resource|null $link mysql link
  277. *
  278. * @return string|bool $error or false
  279. */
  280. public function getError($link)
  281. {
  282. $GLOBALS['errno'] = 0;
  283. if (null !== $link && false !== $link) {
  284. $error_number = mysql_errno($link);
  285. $error_message = mysql_error($link);
  286. } else {
  287. $error_number = mysql_errno();
  288. $error_message = mysql_error();
  289. }
  290. if (0 == $error_number) {
  291. return false;
  292. }
  293. // keep the error number for further check after
  294. // the call to getError()
  295. $GLOBALS['errno'] = $error_number;
  296. return $GLOBALS['dbi']->formatError($error_number, $error_message);
  297. }
  298. /**
  299. * returns the number of rows returned by last query
  300. *
  301. * @param resource $result MySQL result
  302. *
  303. * @return string|int
  304. */
  305. public function numRows($result)
  306. {
  307. if (is_bool($result)) {
  308. return 0;
  309. }
  310. return mysql_num_rows($result);
  311. }
  312. /**
  313. * returns the number of rows affected by last query
  314. *
  315. * @param resource|null $link the mysql object
  316. *
  317. * @return int
  318. */
  319. public function affectedRows($link)
  320. {
  321. return mysql_affected_rows($link);
  322. }
  323. /**
  324. * returns metainfo for fields in $result
  325. *
  326. * @param resource $result MySQL result
  327. *
  328. * @return array meta info for fields in $result
  329. *
  330. * @todo add missing keys like in mysqli_query (decimals)
  331. */
  332. public function getFieldsMeta($result)
  333. {
  334. $fields = array();
  335. $num_fields = mysql_num_fields($result);
  336. for ($i = 0; $i < $num_fields; $i++) {
  337. $field = mysql_fetch_field($result, $i);
  338. $field->flags = mysql_field_flags($result, $i);
  339. $field->orgtable = mysql_field_table($result, $i);
  340. $field->orgname = mysql_field_name($result, $i);
  341. $fields[] = $field;
  342. }
  343. return $fields;
  344. }
  345. /**
  346. * return number of fields in given $result
  347. *
  348. * @param resource $result MySQL result
  349. *
  350. * @return int field count
  351. */
  352. public function numFields($result)
  353. {
  354. return mysql_num_fields($result);
  355. }
  356. /**
  357. * returns the length of the given field $i in $result
  358. *
  359. * @param resource $result MySQL result
  360. * @param int $i field
  361. *
  362. * @return int length of field
  363. */
  364. public function fieldLen($result, $i)
  365. {
  366. return mysql_field_len($result, $i);
  367. }
  368. /**
  369. * returns name of $i. field in $result
  370. *
  371. * @param resource $result MySQL result
  372. * @param int $i field
  373. *
  374. * @return string name of $i. field in $result
  375. */
  376. public function fieldName($result, $i)
  377. {
  378. return mysql_field_name($result, $i);
  379. }
  380. /**
  381. * returns concatenated string of human readable field flags
  382. *
  383. * @param resource $result MySQL result
  384. * @param int $i field
  385. *
  386. * @return string field flags
  387. */
  388. public function fieldFlags($result, $i)
  389. {
  390. return mysql_field_flags($result, $i);
  391. }
  392. /**
  393. * Store the result returned from multi query
  394. *
  395. * @param resource $result MySQL result
  396. *
  397. * @return false
  398. */
  399. public function storeResult($result)
  400. {
  401. return false;
  402. }
  403. /**
  404. * returns properly escaped string for use in MySQL queries
  405. *
  406. * @param mixed $link database link
  407. * @param string $str string to be escaped
  408. *
  409. * @return string a MySQL escaped string
  410. */
  411. public function escapeString($link, $str)
  412. {
  413. return mysql_real_escape_string($str, $link);
  414. }
  415. }