db; $dbh = new PDO("mysql:host={$params->host}; port={$params->port}; dbname={$params->name}", $params->user, $params->password); $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->exec("SET NAMES utf8"); } catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage() . "\n"; die("connect to db failed.\n"); } $tablesExists = $dbh->query('SHOW TABLES')->fetchAll(); foreach($tablesExists as $key => $table) $tablesExists[$key] = current((array)$table); $tablesExists = array_flip($tablesExists); foreach($tables2Rename as $oldTable => $newTable) { if(isset($tablesExists[$newTable])) { echo "Has existed table '$newTable'\n"; } elseif(!isset($tablesExists[$oldTable])) { echo "No found table '$oldTable'\n"; } else { $dbh->query("RENAME TABLE `$oldTable` TO `$newTable`"); echo "RENAME TABLE `$oldTable` TO `$newTable`\n"; } } echo "Finish!\n";