install.mysql.inc

File

drupal-6.x/includes/install.mysql.inc
View source
  1. <?php
  2. // MySQL specific install functions
  3. /**
  4. * Check if MySQL is available.
  5. *
  6. * @return
  7. * TRUE/FALSE
  8. */
  9. function mysql_is_available() {
  10. return function_exists('mysql_connect');
  11. }
  12. /**
  13. * Check if we can connect to MySQL.
  14. *
  15. * @return
  16. * TRUE/FALSE
  17. */
  18. function drupal_test_mysql($url, &$success) {
  19. if (!mysql_is_available()) {
  20. drupal_set_message(st('PHP MySQL 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. // Allow for non-standard MySQL port.
  30. if (isset($url['port'])) {
  31. $url['host'] = $url['host'] .':'. $url['port'];
  32. }
  33. // Test connecting to the database.
  34. $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2);
  35. if (!$connection) {
  36. 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' => mysql_error())), 'error');
  37. return FALSE;
  38. }
  39. // Test selecting the database.
  40. if (!mysql_select_db(substr($url['path'], 1))) {
  41. 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' => mysql_error())), 'error');
  42. return FALSE;
  43. }
  44. $success = array('CONNECT');
  45. // Test CREATE.
  46. $query = 'CREATE TABLE drupal_install_test (id int NULL)';
  47. $result = mysql_query($query);
  48. if ($error = mysql_error()) {
  49. 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');
  50. return FALSE;
  51. }
  52. $err = FALSE;
  53. $success[] = 'SELECT';
  54. $success[] = 'CREATE';
  55. // Test INSERT.
  56. $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
  57. $result = mysql_query($query);
  58. if ($error = mysql_error()) {
  59. 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');
  60. $err = TRUE;
  61. }
  62. else {
  63. $success[] = 'INSERT';
  64. }
  65. // Test UPDATE.
  66. $query = 'UPDATE drupal_install_test SET id = 2';
  67. $result = mysql_query($query);
  68. if ($error = mysql_error()) {
  69. 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');
  70. $err = TRUE;
  71. }
  72. else {
  73. $success[] = 'UPDATE';
  74. }
  75. // Test DELETE.
  76. $query = 'DELETE FROM drupal_install_test';
  77. $result = mysql_query($query);
  78. if ($error = mysql_error()) {
  79. 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');
  80. $err = TRUE;
  81. }
  82. else {
  83. $success[] = 'DELETE';
  84. }
  85. // Test DROP.
  86. $query = 'DROP TABLE drupal_install_test';
  87. $result = mysql_query($query);
  88. if ($error = mysql_error()) {
  89. 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');
  90. $err = TRUE;
  91. }
  92. else {
  93. $success[] = 'DROP';
  94. }
  95. if ($err) {
  96. return FALSE;
  97. }
  98. mysql_close($connection);
  99. return TRUE;
  100. }