Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. CREATE DATABASE IF NOT EXISTS 'drupal_mydatabase';
  2. USE 'drupal_mydatabase';
  3.  
  4. <?php
  5. $db_server = "localhost"; // hostname MySQL server
  6. $db_username = "username"; // username MySQL server
  7. $db_password = "password"; // password MySQL server
  8. $db_name = "database"; // database name
  9.  
  10. $pattern = "pattern_"; // search string for example: "drpl_"
  11. $new_pattern = "new_pattern_"; // replacement string.
  12. // can be empty. For example "" for no db prefix
  13.  
  14. // login to MySQL server
  15. $link = mysql_connect( $db_server, $db_username, $db_password);
  16.  
  17. if (!$link)
  18. {
  19. die('Could not connect: ' . mysql_error());
  20. }
  21.  
  22. // list all tables in the database containing the search pattern
  23. $sql = "SHOW TABLES FROM `" . $db_name . "`";
  24. $sql .= " LIKE '%" . $pattern . "%'";
  25.  
  26. $result = mysql_query ( $sql, $link );
  27. if (!$result)
  28. {
  29. die("Invalid query: " . mysql_error( $link ));
  30. }
  31.  
  32. $renamed = 0;
  33. $failed = 0;
  34.  
  35. while ( $row = mysql_fetch_array ($result) )
  36. {
  37. // rename every table by replacing the search pattern
  38. // with a new pattern
  39. $table_name = $row[0];
  40. $new_table_name = str_replace ( $pattern, $new_pattern, $table_name);
  41.  
  42. $sql = "RENAME TABLE `" . $db_name . "`.`" . $table_name . "`";
  43. $sql .= " TO `" . $db_name . "`.`" . $new_table_name . "`";
  44.  
  45. $result_rename = mysql_query ( $sql, $link );
  46. if ($result_rename)
  47. {
  48. echo "Table `" . $table_name . "` renamed to :`";
  49. echo $new_table_name . "`.n";
  50. $renamed++;
  51. }
  52. else
  53. {
  54. // notify when the renaming failed and show reason why
  55. echo "Renaming of table `" . $table_name . "` has failed: ";
  56. echo mysql_error( $link ) . "n";
  57. $failed++;
  58. }
  59. }
  60.  
  61. echo $renamed . " tables were renamed, " . $failed . " failed.n";
  62.  
  63. // close connection to MySQL server
  64. mysql_close( $link );
  65. ?>
  66.  
  67. array (
  68. 'database' => 'sample_drupal_db',
  69. 'username' => 'sample_drupal_usr',
  70. 'password' => 'sample_drupal_pwd',
  71. 'host' => 'localhost',
  72. 'port' => '',
  73. 'driver' => 'mysql',
  74. 'prefix' => 'prefix_', // <<== prefix here
  75. ),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement