DbiMysqli.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. /**
  3. * Interface to the MySQL Improved extension (MySQLi)
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Dbal;
  7. use mysqli;
  8. use mysqli_result;
  9. use mysqli_stmt;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Query\Utilities;
  12. use stdClass;
  13. use function mysqli_report;
  14. use const E_USER_WARNING;
  15. use const MYSQLI_ASSOC;
  16. use const MYSQLI_AUTO_INCREMENT_FLAG;
  17. use const MYSQLI_BLOB_FLAG;
  18. use const MYSQLI_BOTH;
  19. use const MYSQLI_CLIENT_COMPRESS;
  20. use const MYSQLI_CLIENT_SSL;
  21. use const MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
  22. use const MYSQLI_ENUM_FLAG;
  23. use const MYSQLI_MULTIPLE_KEY_FLAG;
  24. use const MYSQLI_NOT_NULL_FLAG;
  25. use const MYSQLI_NUM;
  26. use const MYSQLI_NUM_FLAG;
  27. use const MYSQLI_OPT_LOCAL_INFILE;
  28. use const MYSQLI_OPT_SSL_VERIFY_SERVER_CERT;
  29. use const MYSQLI_PART_KEY_FLAG;
  30. use const MYSQLI_PRI_KEY_FLAG;
  31. use const MYSQLI_REPORT_OFF;
  32. use const MYSQLI_SET_FLAG;
  33. use const MYSQLI_STORE_RESULT;
  34. use const MYSQLI_TIMESTAMP_FLAG;
  35. use const MYSQLI_TYPE_BIT;
  36. use const MYSQLI_TYPE_BLOB;
  37. use const MYSQLI_TYPE_DATE;
  38. use const MYSQLI_TYPE_DATETIME;
  39. use const MYSQLI_TYPE_DECIMAL;
  40. use const MYSQLI_TYPE_DOUBLE;
  41. use const MYSQLI_TYPE_ENUM;
  42. use const MYSQLI_TYPE_FLOAT;
  43. use const MYSQLI_TYPE_GEOMETRY;
  44. use const MYSQLI_TYPE_INT24;
  45. use const MYSQLI_TYPE_JSON;
  46. use const MYSQLI_TYPE_LONG;
  47. use const MYSQLI_TYPE_LONG_BLOB;
  48. use const MYSQLI_TYPE_LONGLONG;
  49. use const MYSQLI_TYPE_MEDIUM_BLOB;
  50. use const MYSQLI_TYPE_NEWDATE;
  51. use const MYSQLI_TYPE_NEWDECIMAL;
  52. use const MYSQLI_TYPE_NULL;
  53. use const MYSQLI_TYPE_SET;
  54. use const MYSQLI_TYPE_SHORT;
  55. use const MYSQLI_TYPE_STRING;
  56. use const MYSQLI_TYPE_TIME;
  57. use const MYSQLI_TYPE_TIMESTAMP;
  58. use const MYSQLI_TYPE_TINY;
  59. use const MYSQLI_TYPE_TINY_BLOB;
  60. use const MYSQLI_TYPE_VAR_STRING;
  61. use const MYSQLI_TYPE_YEAR;
  62. use const MYSQLI_UNIQUE_KEY_FLAG;
  63. use const MYSQLI_UNSIGNED_FLAG;
  64. use const MYSQLI_USE_RESULT;
  65. use const MYSQLI_ZEROFILL_FLAG;
  66. use function define;
  67. use function defined;
  68. use function implode;
  69. use function is_array;
  70. use function is_bool;
  71. use function mysqli_init;
  72. use function stripos;
  73. use function trigger_error;
  74. use const PHP_VERSION_ID;
  75. use function mysqli_get_client_info;
  76. /**
  77. * Interface to the MySQL Improved extension (MySQLi)
  78. */
  79. class DbiMysqli implements DbiExtension
  80. {
  81. /** @var array */
  82. private static $flagNames = [
  83. MYSQLI_NUM_FLAG => 'num',
  84. MYSQLI_PART_KEY_FLAG => 'part_key',
  85. MYSQLI_SET_FLAG => 'set',
  86. MYSQLI_TIMESTAMP_FLAG => 'timestamp',
  87. MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment',
  88. MYSQLI_ENUM_FLAG => 'enum',
  89. MYSQLI_ZEROFILL_FLAG => 'zerofill',
  90. MYSQLI_UNSIGNED_FLAG => 'unsigned',
  91. MYSQLI_BLOB_FLAG => 'blob',
  92. MYSQLI_MULTIPLE_KEY_FLAG => 'multiple_key',
  93. MYSQLI_UNIQUE_KEY_FLAG => 'unique_key',
  94. MYSQLI_PRI_KEY_FLAG => 'primary_key',
  95. MYSQLI_NOT_NULL_FLAG => 'not_null',
  96. ];
  97. /**
  98. * connects to the database server
  99. *
  100. * @param string $user mysql user name
  101. * @param string $password mysql user password
  102. * @param array $server host/port/socket/persistent
  103. *
  104. * @return mysqli|bool false on error or a mysqli object on success
  105. */
  106. public function connect($user, $password, array $server)
  107. {
  108. if ($server) {
  109. $server['host'] = empty($server['host'])
  110. ? 'localhost'
  111. : $server['host'];
  112. }
  113. mysqli_report(MYSQLI_REPORT_OFF);
  114. $mysqli = mysqli_init();
  115. $client_flags = 0;
  116. /* Optionally compress connection */
  117. if ($server['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
  118. $client_flags |= MYSQLI_CLIENT_COMPRESS;
  119. }
  120. /* Optionally enable SSL */
  121. if ($server['ssl']) {
  122. $client_flags |= MYSQLI_CLIENT_SSL;
  123. if (! empty($server['ssl_key']) ||
  124. ! empty($server['ssl_cert']) ||
  125. ! empty($server['ssl_ca']) ||
  126. ! empty($server['ssl_ca_path']) ||
  127. ! empty($server['ssl_ciphers'])
  128. ) {
  129. $mysqli->ssl_set(
  130. $server['ssl_key'] ?? '',
  131. $server['ssl_cert'] ?? '',
  132. $server['ssl_ca'] ?? '',
  133. $server['ssl_ca_path'] ?? '',
  134. $server['ssl_ciphers'] ?? ''
  135. );
  136. }
  137. /*
  138. * disables SSL certificate validation on mysqlnd for MySQL 5.6 or later
  139. * @link https://bugs.php.net/bug.php?id=68344
  140. * @link https://github.com/phpmyadmin/phpmyadmin/pull/11838
  141. */
  142. if (! $server['ssl_verify']) {
  143. $mysqli->options(
  144. MYSQLI_OPT_SSL_VERIFY_SERVER_CERT,
  145. $server['ssl_verify']
  146. );
  147. $client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
  148. }
  149. }
  150. if ($GLOBALS['cfg']['PersistentConnections']) {
  151. $host = 'p:' . $server['host'];
  152. } else {
  153. $host = $server['host'];
  154. }
  155. $return_value = $mysqli->real_connect(
  156. $host,
  157. $user,
  158. $password,
  159. '',
  160. $server['port'],
  161. (string) $server['socket'],
  162. $client_flags
  163. );
  164. if ($return_value === false || $return_value === null) {
  165. /*
  166. * Switch to SSL if server asked us to do so, unfortunately
  167. * there are more ways MySQL server can tell this:
  168. *
  169. * - MySQL 8.0 and newer should return error 3159
  170. * - #2001 - SSL Connection is required. Please specify SSL options and retry.
  171. * - #9002 - SSL connection is required. Please specify SSL options and retry.
  172. */
  173. $error_number = $mysqli->connect_errno;
  174. $error_message = $mysqli->connect_error;
  175. if (! $server['ssl']
  176. && ($error_number == 3159
  177. || (($error_number == 2001 || $error_number == 9002)
  178. && stripos($error_message, 'SSL Connection is required') !== false))
  179. ) {
  180. trigger_error(
  181. __('SSL connection enforced by server, automatically enabling it.'),
  182. E_USER_WARNING
  183. );
  184. $server['ssl'] = true;
  185. return self::connect($user, $password, $server);
  186. }
  187. return false;
  188. }
  189. if (defined('PMA_ENABLE_LDI')) {
  190. $mysqli->options(MYSQLI_OPT_LOCAL_INFILE, true);
  191. } else {
  192. $mysqli->options(MYSQLI_OPT_LOCAL_INFILE, false);
  193. }
  194. return $mysqli;
  195. }
  196. /**
  197. * selects given database
  198. *
  199. * @param string $databaseName database name to select
  200. * @param mysqli $mysqli the mysqli object
  201. *
  202. * @return bool
  203. */
  204. public function selectDb($databaseName, $mysqli)
  205. {
  206. return $mysqli->select_db($databaseName);
  207. }
  208. /**
  209. * runs a query and returns the result
  210. *
  211. * @param string $query query to execute
  212. * @param mysqli $mysqli mysqli object
  213. * @param int $options query options
  214. *
  215. * @return mysqli_result|bool
  216. */
  217. public function realQuery($query, $mysqli, $options)
  218. {
  219. if ($options == ($options | DatabaseInterface::QUERY_STORE)) {
  220. $method = MYSQLI_STORE_RESULT;
  221. } elseif ($options == ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
  222. $method = MYSQLI_USE_RESULT;
  223. } else {
  224. $method = 0;
  225. }
  226. return $mysqli->query($query, $method);
  227. }
  228. /**
  229. * Run the multi query and output the results
  230. *
  231. * @param mysqli $mysqli mysqli object
  232. * @param string $query multi query statement to execute
  233. *
  234. * @return bool
  235. */
  236. public function realMultiQuery($mysqli, $query)
  237. {
  238. return $mysqli->multi_query($query);
  239. }
  240. /**
  241. * returns array of rows with associative and numeric keys from $result
  242. *
  243. * @param mysqli_result $result result set identifier
  244. */
  245. public function fetchArray($result): ?array
  246. {
  247. if (! $result instanceof mysqli_result) {
  248. return null;
  249. }
  250. return $result->fetch_array(MYSQLI_BOTH);
  251. }
  252. /**
  253. * returns array of rows with associative keys from $result
  254. *
  255. * @param mysqli_result $result result set identifier
  256. */
  257. public function fetchAssoc($result): ?array
  258. {
  259. if (! $result instanceof mysqli_result) {
  260. return null;
  261. }
  262. return $result->fetch_array(MYSQLI_ASSOC);
  263. }
  264. /**
  265. * returns array of rows with numeric keys from $result
  266. *
  267. * @param mysqli_result $result result set identifier
  268. */
  269. public function fetchRow($result): ?array
  270. {
  271. if (! $result instanceof mysqli_result) {
  272. return null;
  273. }
  274. return $result->fetch_array(MYSQLI_NUM);
  275. }
  276. /**
  277. * Adjusts the result pointer to an arbitrary row in the result
  278. *
  279. * @param mysqli_result $result database result
  280. * @param int $offset offset to seek
  281. *
  282. * @return bool true on success, false on failure
  283. */
  284. public function dataSeek($result, $offset)
  285. {
  286. return $result->data_seek($offset);
  287. }
  288. /**
  289. * Frees memory associated with the result
  290. *
  291. * @param mysqli_result $result database result
  292. *
  293. * @return void
  294. */
  295. public function freeResult($result)
  296. {
  297. if (! ($result instanceof mysqli_result)) {
  298. return;
  299. }
  300. $result->close();
  301. }
  302. /**
  303. * Check if there are any more query results from a multi query
  304. *
  305. * @param mysqli $mysqli the mysqli object
  306. *
  307. * @return bool true or false
  308. */
  309. public function moreResults($mysqli)
  310. {
  311. return $mysqli->more_results();
  312. }
  313. /**
  314. * Prepare next result from multi_query
  315. *
  316. * @param mysqli $mysqli the mysqli object
  317. *
  318. * @return bool true or false
  319. */
  320. public function nextResult($mysqli)
  321. {
  322. return $mysqli->next_result();
  323. }
  324. /**
  325. * Store the result returned from multi query
  326. *
  327. * @param mysqli $mysqli the mysqli object
  328. *
  329. * @return mysqli_result|bool false when empty results / result set when not empty
  330. */
  331. public function storeResult($mysqli)
  332. {
  333. return $mysqli->store_result();
  334. }
  335. /**
  336. * Returns a string representing the type of connection used
  337. *
  338. * @param mysqli $mysqli mysql link
  339. *
  340. * @return string type of connection used
  341. */
  342. public function getHostInfo($mysqli)
  343. {
  344. return $mysqli->host_info;
  345. }
  346. /**
  347. * Returns the version of the MySQL protocol used
  348. *
  349. * @param mysqli $mysqli mysql link
  350. *
  351. * @return string version of the MySQL protocol used
  352. */
  353. public function getProtoInfo($mysqli)
  354. {
  355. return $mysqli->protocol_version;
  356. }
  357. /**
  358. * returns a string that represents the client library version
  359. *
  360. * @param mysqli $mysqli mysql link
  361. *
  362. * @return string MySQL client library version
  363. */
  364. public function getClientInfo($mysqli)
  365. {
  366. // See: https://github.com/phpmyadmin/phpmyadmin/issues/16911
  367. if (PHP_VERSION_ID < 80100) {
  368. return $mysqli->get_client_info();
  369. }
  370. return mysqli_get_client_info();
  371. }
  372. /**
  373. * returns last error message or false if no errors occurred
  374. *
  375. * @param mysqli $mysqli mysql link
  376. *
  377. * @return string|bool error or false
  378. */
  379. public function getError($mysqli)
  380. {
  381. $GLOBALS['errno'] = 0;
  382. if ($mysqli !== null && $mysqli !== false) {
  383. $error_number = $mysqli->errno;
  384. $error_message = $mysqli->error;
  385. } else {
  386. $error_number = $mysqli->connect_errno;
  387. $error_message = $mysqli->connect_error;
  388. }
  389. if ($error_number == 0) {
  390. return false;
  391. }
  392. // keep the error number for further check after
  393. // the call to getError()
  394. $GLOBALS['errno'] = $error_number;
  395. return Utilities::formatError($error_number, $error_message);
  396. }
  397. /**
  398. * returns the number of rows returned by last query
  399. *
  400. * @param mysqli_result $result result set identifier
  401. *
  402. * @return string|int
  403. */
  404. public function numRows($result)
  405. {
  406. // see the note for tryQuery();
  407. if (is_bool($result)) {
  408. return 0;
  409. }
  410. return $result->num_rows;
  411. }
  412. /**
  413. * returns the number of rows affected by last query
  414. *
  415. * @param mysqli $mysqli the mysqli object
  416. *
  417. * @return int
  418. */
  419. public function affectedRows($mysqli)
  420. {
  421. return $mysqli->affected_rows;
  422. }
  423. /**
  424. * returns meta info for fields in $result
  425. *
  426. * @param mysqli_result $result result set identifier
  427. *
  428. * @return array|bool meta info for fields in $result
  429. */
  430. public function getFieldsMeta($result)
  431. {
  432. if (! $result instanceof mysqli_result) {
  433. return false;
  434. }
  435. // Issue #16043 - client API mysqlnd seem not to have MYSQLI_TYPE_JSON defined
  436. if (! defined('MYSQLI_TYPE_JSON')) {
  437. define('MYSQLI_TYPE_JSON', 245);
  438. }
  439. // Build an associative array for a type look up
  440. $typeAr = [];
  441. $typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
  442. $typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
  443. $typeAr[MYSQLI_TYPE_BIT] = 'int';
  444. $typeAr[MYSQLI_TYPE_TINY] = 'int';
  445. $typeAr[MYSQLI_TYPE_SHORT] = 'int';
  446. $typeAr[MYSQLI_TYPE_LONG] = 'int';
  447. $typeAr[MYSQLI_TYPE_FLOAT] = 'real';
  448. $typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
  449. $typeAr[MYSQLI_TYPE_NULL] = 'null';
  450. $typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
  451. $typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
  452. $typeAr[MYSQLI_TYPE_INT24] = 'int';
  453. $typeAr[MYSQLI_TYPE_DATE] = 'date';
  454. $typeAr[MYSQLI_TYPE_TIME] = 'time';
  455. $typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
  456. $typeAr[MYSQLI_TYPE_YEAR] = 'year';
  457. $typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
  458. $typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
  459. $typeAr[MYSQLI_TYPE_SET] = 'unknown';
  460. $typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
  461. $typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
  462. $typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
  463. $typeAr[MYSQLI_TYPE_BLOB] = 'blob';
  464. $typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
  465. $typeAr[MYSQLI_TYPE_STRING] = 'string';
  466. // MySQL returns MYSQLI_TYPE_STRING for CHAR
  467. // and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
  468. // so this would override TINYINT and mark all TINYINT as string
  469. // see https://github.com/phpmyadmin/phpmyadmin/issues/8569
  470. //$typeAr[MYSQLI_TYPE_CHAR] = 'string';
  471. $typeAr[MYSQLI_TYPE_GEOMETRY] = 'geometry';
  472. $typeAr[MYSQLI_TYPE_BIT] = 'bit';
  473. $typeAr[MYSQLI_TYPE_JSON] = 'json';
  474. $fields = $result->fetch_fields();
  475. if (! is_array($fields)) {
  476. return false;
  477. }
  478. foreach ($fields as $k => $field) {
  479. $fields[$k]->_type = $field->type;
  480. $fields[$k]->type = $typeAr[$field->type];
  481. $fields[$k]->_flags = $field->flags;
  482. $fields[$k]->flags = $this->fieldFlags($result, $k);
  483. // Enhance the field objects for mysql-extension compatibility
  484. //$flags = explode(' ', $fields[$k]->flags);
  485. //array_unshift($flags, 'dummy');
  486. $fields[$k]->multiple_key
  487. = (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
  488. $fields[$k]->primary_key
  489. = (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
  490. $fields[$k]->unique_key
  491. = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
  492. $fields[$k]->not_null
  493. = (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
  494. $fields[$k]->unsigned
  495. = (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
  496. $fields[$k]->zerofill
  497. = (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
  498. $fields[$k]->numeric
  499. = (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
  500. $fields[$k]->blob
  501. = (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
  502. }
  503. return $fields;
  504. }
  505. /**
  506. * return number of fields in given $result
  507. *
  508. * @param mysqli_result $result result set identifier
  509. *
  510. * @return int field count
  511. */
  512. public function numFields($result)
  513. {
  514. return $result->field_count;
  515. }
  516. /**
  517. * returns the length of the given field $i in $result
  518. *
  519. * @param mysqli_result $result result set identifier
  520. * @param int $i field
  521. *
  522. * @return int|bool length of field
  523. */
  524. public function fieldLen($result, $i)
  525. {
  526. if ($i >= $this->numFields($result)) {
  527. return false;
  528. }
  529. /** @var stdClass $fieldDefinition */
  530. $fieldDefinition = $result->fetch_field_direct($i);
  531. if ($fieldDefinition !== false) {
  532. return $fieldDefinition->length;
  533. }
  534. return false;
  535. }
  536. /**
  537. * returns name of $i. field in $result
  538. *
  539. * @param mysqli_result $result result set identifier
  540. * @param int $i field
  541. *
  542. * @return string name of $i. field in $result
  543. */
  544. public function fieldName($result, $i)
  545. {
  546. if ($i >= $this->numFields($result)) {
  547. return '';
  548. }
  549. /** @var stdClass $fieldDefinition */
  550. $fieldDefinition = $result->fetch_field_direct($i);
  551. if ($fieldDefinition !== false) {
  552. return $fieldDefinition->name;
  553. }
  554. return '';
  555. }
  556. /**
  557. * returns concatenated string of human readable field flags
  558. *
  559. * @param mysqli_result $result result set identifier
  560. * @param int $i field
  561. *
  562. * @return string|false field flags
  563. */
  564. public function fieldFlags($result, $i)
  565. {
  566. if ($i >= $this->numFields($result)) {
  567. return false;
  568. }
  569. /** @var stdClass|false $fieldDefinition */
  570. $fieldDefinition = $result->fetch_field_direct($i);
  571. if ($fieldDefinition === false) {
  572. return '';
  573. }
  574. $type = $fieldDefinition->type;
  575. $charsetNumber = $fieldDefinition->charsetnr;
  576. $fieldDefinitionFlags = $fieldDefinition->flags;
  577. $flags = [];
  578. foreach (self::$flagNames as $flag => $name) {
  579. if (! ($fieldDefinitionFlags & $flag)) {
  580. continue;
  581. }
  582. $flags[] = $name;
  583. }
  584. // See https://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
  585. // to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
  586. // but instead the charsetnr member of the MYSQL_FIELD
  587. // structure. Watch out: some types like DATE returns 63 in charsetnr
  588. // so we have to check also the type.
  589. // Unfortunately there is no equivalent in the mysql extension.
  590. if (($type == MYSQLI_TYPE_TINY_BLOB || $type == MYSQLI_TYPE_BLOB
  591. || $type == MYSQLI_TYPE_MEDIUM_BLOB || $type == MYSQLI_TYPE_LONG_BLOB
  592. || $type == MYSQLI_TYPE_VAR_STRING || $type == MYSQLI_TYPE_STRING)
  593. && $charsetNumber == 63
  594. ) {
  595. $flags[] = 'binary';
  596. }
  597. return implode(' ', $flags);
  598. }
  599. /**
  600. * returns properly escaped string for use in MySQL queries
  601. *
  602. * @param mysqli $mysqli database link
  603. * @param string $string string to be escaped
  604. *
  605. * @return string a MySQL escaped string
  606. */
  607. public function escapeString($mysqli, $string)
  608. {
  609. return $mysqli->real_escape_string($string);
  610. }
  611. /**
  612. * Prepare an SQL statement for execution.
  613. *
  614. * @param mysqli $mysqli database link
  615. * @param string $query The query, as a string.
  616. *
  617. * @return mysqli_stmt|false A statement object or false.
  618. */
  619. public function prepare($mysqli, string $query)
  620. {
  621. return $mysqli->prepare($query);
  622. }
  623. }