用模式重命名mysql中的多个表

时间:2018-02-14 18:48:03

标签: php mysql

此代码不是上述链接的副本,因为问题不在于语法而是表解析 您好,这是我用来读取具有username_CentralTable模式的表名称的scirpt,并将其更改为UID_CentralTable但不断出错。

<?php   
$db_server = "localhost";    // hostname MySQL server   
$db_username = "username";   // username MySQL server
$db_password = "password";   // password MySQL server   
$db_name = "database";       // database name       

$pattern = "_CentralTable";          // search string
$new_pattern = "new_pattern_";  // replacement string, 
                                // can be empty

// login to MySQL server
$link = mysqli_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 ( $sql, $link );
if (!$result)
{
  die("Invalid query: " . mysqli_error( $link ));
}

$renamed = 0;
$failed = 0;

while ( $row = mysqli_fetch_array ($result) )
{
  // rename every table by replacing the search pattern 
  // with a new pattern
  $table_name = $row[0];
  $new_table_name = str_replace ( $pattern, $new_pattern, $table_name);

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

  $result_rename = mysqli_query ( $sql, $link );
  if ($result_rename)
  {
    echo "Table " . $table_name . " renamed to :"; 
    echo $new_table_name . ".\n"; 
    $renamed++;
  }
  else
  {
    // notify when the renaming failed and show reason why
    echo "Renaming of table " . $table_name . " has failed: ";
    echo mysql_error( $link ) . "\n";
    $failed++;
  }
}

echo $renamed . " tables were renamed, " . $failed . " failed.\n";

// close connection to MySQL server
mysqli_close( $link );
?>

请帮我处理代码,因为我得到的错误是

  

解析错误:语法错误,意外的'$ result'(T_VARIABLE),期待','或';'在/Users/Swayam/Desktop/TableRename.php第24行

如果我最初打印回声,我得到:

SHOW TABLES FROM `c10mill2_edtopia` LIKE '%_CentralTable%'Swayam-MBP:Desktop Swayam$

0 个答案:

没有答案