Imported drupal-5.3
[drupal.git] / includes / install.mysql.inc
blobfe7b0fee35db49986377fcf340423ca77d08ed3e
1 <?php
2 // $Id: install.mysql.inc,v 1.4 2006/12/27 13:02:34 drumm Exp $
4 // MySQL specific install functions
6 /**
7  * Check if MySQL is available.
8  *
9  * @return
10  *  TRUE/FALSE
11  */
12 function mysql_is_available() {
13   return function_exists('mysql_connect');
16 /**
17  * Check if we can connect to MySQL.
18  *
19  * @return
20  *  TRUE/FALSE
21  */
22 function drupal_test_mysql($url, &$success) {
23   if (!mysql_is_available()) {
24     drupal_set_message(st('PHP MySQL support not enabled.'), 'error');
25     return FALSE;
26   }
28   $url = parse_url($url);
30   // Decode url-encoded information in the db connection string.
31   $url['user'] = urldecode($url['user']);
32   $url['pass'] = urldecode($url['pass']);
33   $url['host'] = urldecode($url['host']);
34   $url['path'] = urldecode($url['path']);
36   // Allow for non-standard MySQL port.
37   if (isset($url['port'])) {
38      $url['host'] = $url['host'] .':'. $url['port'];
39   }
41   // Test connecting to the database.
42   $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2);
43   if (!$connection) {
44     drupal_set_message(st('Failure 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');
45     return FALSE;
46   }
48   // Test selecting the database.
49   if (!mysql_select_db(substr($url['path'], 1))) {
50     drupal_set_message(st('We were able to connect to the MySQL database server (which means your username and password are valid) but not able to select your database. 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');
51     return FALSE;
52   }
54   $success = array('CONNECT');
56   // Test CREATE.
57   $query = 'CREATE TABLE drupal_install_test (id int NULL)';
58   $result = mysql_query($query);
59   if ($error = mysql_error()) {
60     drupal_set_message(st('We were unable 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');
61     return FALSE;
62   }
63   $err = FALSE;
64   $success[] = 'SELECT';
65   $success[] = 'CREATE';
67   // Test INSERT.
68   $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
69   $result = mysql_query($query);
70   if ($error = mysql_error()) {
71     drupal_set_message(st('We were unable 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');
72     $err = TRUE;
73   }
74   else {
75     $success[] = 'INSERT';
76   }
78   // Test UPDATE.
79   $query = 'UPDATE drupal_install_test SET id = 2';
80   $result = mysql_query($query);
81   if ($error = mysql_error()) {
82     drupal_set_message(st('We were unable 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');
83     $err = TRUE;
84   }
85   else {
86     $success[] = 'UPDATE';
87   }
89   // Test LOCK.
90   $query = 'LOCK TABLES drupal_install_test WRITE';
91   $result = mysql_query($query);
92   if ($error = mysql_error()) {
93     drupal_set_message(st('We were unable to lock a test table on your MySQL database server. We tried locking a table with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
94     $err = TRUE;
95   }
96   else {
97     $success[] = 'LOCK';
98   }
100   // Test UNLOCK.
101   $query = 'UNLOCK TABLES';
102   $result = mysql_query($query);
103   if ($error = mysql_error()) {
104     drupal_set_message(st('We were unable to unlock a test table on your MySQL database server. We tried unlocking a table with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
105     $err = TRUE;
106   }
107   else {
108     $success[] = 'UNLOCK';
109   }
111   // Test DELETE.
112   $query = 'DELETE FROM drupal_install_test';
113   $result = mysql_query($query);
114   if ($error = mysql_error()) {
115     drupal_set_message(st('We were unable 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');
116     $err = TRUE;
117   }
118   else {
119     $success[] = 'DELETE';
120   }
122   // Test DROP.
123   $query = 'DROP TABLE drupal_install_test';
124   $result = mysql_query($query);
125   if ($error = mysql_error()) {
126     drupal_set_message(st('We were unable 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');
127     $err = TRUE;
128   }
129   else {
130     $success[] = 'DROP';
131   }
133   if ($err) {
134     return FALSE;
135   }
137   mysql_close($connection);
138   return TRUE;