Переименование префикса у таблиц mysql

<?php
ini_set('display_errors', 1);
$db_server = "127.0.0.1";    // hostname MySQL server
$db_username = "";   // username MySQL server
$db_password = "";   // password MySQL server
$db_name = "";       // database name

$pattern = "modx_";          // search string, can be empty
$new_pattern = "prifix_";  // replacement string, can be empty

$link = mysqli_connect($db_server, $db_username, $db_password, $db_name);
mysqli_query($link, "set names utf 8");

// login to MySQL server
#$link = mysql_connect( $db_server, $db_username, $db_password);

if (!$link)
{
    die('Could not connect: ' . mysql_error());
}

// list all tables in the database containing the search pattern
$sql = "SHOW TABLES FROM `" . $db_name . "`";
$sql .= " LIKE '%" . $pattern . "%'";

$result = mysqli_query($link, $sql);
$renamed = 0;
$failed = 0;

$sqls = array();
while($row = $result->fetch_array())
{
    $table_name = $row[0];

    if (strlen($pattern) > 0 ) {
        // rename every table by replacing the search pattern with a new pattern
        $new_table_name = str_replace ( $pattern, $new_pattern, $table_name);
    } else {
        // rename every table by adding a new pattern
        $new_table_name = $new_pattern.$table_name;
    }

    $sql = "RENAME TABLE `" . $db_name . "`.`" . $table_name . "`";
    $sql .= " TO `" . $db_name . "`.`" . $new_table_name . "`";

    $sqls[] = $sql;
}
mysqli_close( $link );
$res = implode(';<br>', $sqls);

echo '<pre>'; 
print_r($res); die;

// close connection to MySQL server
28 декабря 2017, 03:52    163



Наверх