install.mysqli.inc

File

drupal-6.x/includes/install.mysqli.inc
View source
  1. <?php
  2. // MySQLi specific install functions
  3. /**
  4. * Check if MySQLi is available.
  5. *
  6. * @return
  7. * TRUE/FALSE
  8. */
  9. function mysqli_is_available() {
  10. return function_exists('mysqli_connect');
  11. }
  12. /**
  13. * Check if we can connect to MySQL.
  14. *
  15. * @return
  16. * TRUE/FALSE
  17. */
  18. function drupal_test_mysqli($url, &$success) {
  19. if (!mysqli_is_available()) {
  20. drupal_set_message(st('PHP MySQLi support not enabled.'), 'error');
  21. return FALSE;
  22. }
  23. $url = parse_url($url);
  24. // Decode urlencoded information in the db connection string.
  25. $url['user'] = urldecode($url['user']);
  26. $url['pass'] = isset($url['pass']) ? urldecode($url['pass']) : '';
  27. $url['host'] = urldecode($url['host']);
  28. $url['path'] = urldecode($url['path']);
  29. $connection = mysqli_init();
  30. @mysqli_real_connect($connection, $url['host'], $url['user'], $url['pass'], substr($url['path'], 1), $url['port'], NULL, MYSQLI_CLIENT_FOUND_ROWS);
  31. if (mysqli_connect_errno() >= 2000 || mysqli_connect_errno() == 1045) {
  32. drupal_set_message(st('Failed to connect to your MySQL database server. MySQL reports the following message: %error.<ul><li>Are you sure you have the correct username and password?</li><li>Are you sure that you have typed the correct database hostname?</li><li>Are you sure that the database server is running?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => mysqli_connect_error())), 'error');
  33. return FALSE;
  34. }
  35. // Test selecting the database.
  36. if (mysqli_connect_errno() > 0) {
  37. drupal_set_message(st('Failed to select your database on your MySQL database server, which means the connection username and password are valid, but there is a problem accessing your data. MySQL reports the following message: %error.<ul><li>Are you sure you have the correct database name?</li><li>Are you sure the database exists?</li><li>Are you sure the username has permission to access the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => mysqli_connect_error())), 'error');
  38. return FALSE;
  39. }
  40. $success = array('CONNECT');
  41. // Test CREATE.
  42. $query = 'CREATE TABLE drupal_install_test (id int NULL)';
  43. $result = mysqli_query($connection, $query);
  44. if ($error = mysqli_error($connection)) {
  45. drupal_set_message(st('Failed to create a test table on your MySQL database server with the command %query. MySQL reports the following message: %error.<ul><li>Are you sure the configured username has the necessary MySQL permissions to create tables in the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%query' => $query, '%error' => $error)), 'error');
  46. return FALSE;
  47. }
  48. $err = FALSE;
  49. $success[] = 'SELECT';
  50. $success[] = 'CREATE';
  51. // Test INSERT.
  52. $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
  53. $result = mysqli_query($connection, $query);
  54. if ($error = mysqli_error($connection)) {
  55. drupal_set_message(st('Failed to insert a value into a test table on your MySQL database server. We tried inserting a value with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  56. $err = TRUE;
  57. }
  58. else {
  59. $success[] = 'INSERT';
  60. }
  61. // Test UPDATE.
  62. $query = 'UPDATE drupal_install_test SET id = 2';
  63. $result = mysqli_query($connection, $query);
  64. if ($error = mysqli_error($connection)) {
  65. drupal_set_message(st('Failed to update a value in a test table on your MySQL database server. We tried updating a value with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  66. $err = TRUE;
  67. }
  68. else {
  69. $success[] = 'UPDATE';
  70. }
  71. // Test DELETE.
  72. $query = 'DELETE FROM drupal_install_test';
  73. $result = mysqli_query($connection, $query);
  74. if ($error = mysqli_error($connection)) {
  75. drupal_set_message(st('Failed to delete a value from a test table on your MySQL database server. We tried deleting a value with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  76. $err = TRUE;
  77. }
  78. else {
  79. $success[] = 'DELETE';
  80. }
  81. // Test DROP.
  82. $query = 'DROP TABLE drupal_install_test';
  83. $result = mysqli_query($connection, $query);
  84. if ($error = mysqli_error($connection)) {
  85. drupal_set_message(st('Failed to drop a test table from your MySQL database server. We tried dropping a table with the command %query and MySQL reported the following error %error.', array('%query' => $query, '%error' => $error)), 'error');
  86. $err = TRUE;
  87. }
  88. else {
  89. $success[] = 'DROP';
  90. }
  91. if ($err) {
  92. return FALSE;
  93. }
  94. mysqli_close($connection);
  95. return TRUE;
  96. }