Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-Batch-Konvertierung MySQL-Tabellen-Engine

PHP-Batch-Konvertierung MySQL-Tabellen-Engine

巴扎黑
巴扎黑Original
2016-11-24 10:28:201168Durchsuche

<?php
/**
 * 批量转换Mysql表引擎
 */
error_reporting(E_ALL);
// 数据库连接配置
$host     = &#39;localhost&#39;;
$username = &#39;root&#39;;
$passwd   = &#39;&#39;;
$database = &#39;test&#39;;
// 要转换的库名配置,多库转换增加配置元素即可
$configs  = array($database);
// 转换配置
$convert_rule = array(
    &#39;from&#39; => &#39;InnoDB&#39;,
    &#39;to&#39;   => &#39;MyISAM&#39;
);
mysql_engine_convert();
/**
 * 转换函数
 */
function mysql_engine_convert()
{
    global $host,$username,$passwd,$configs,$convert_rule;
    if ( ($conn = mysql_connect($host, $username, $passwd)) !== false)
    {
        foreach ($configs as $db_name)
        {
            mysql_select_db($db_name) or exit(&#39;Not found db: &#39;. $db_name);
            $tables = mysql_query("SHOW FULL TABLES");
            while ($table = mysql_fetch_row($tables))
            {
                if ($table[1] === &#39;VIEW&#39;) continue;
                $sql = "SHOW TABLE STATUS from {$db_name} where Name=&#39;{$table[0]}&#39; ";
                if ($result = mysql_query($sql))
                {
                    $table_status = mysql_fetch_row($result);
                    
                    if (strtolower($table_status[1]) == strtolower($convert_rule[&#39;from&#39;]))
                        mysql_query("ALTER TABLE {$table[0]} ENGINE = {$convert_rule[&#39;to&#39;]}");
                }
            }
            echo $db_name,&#39;:All tables ENGINE is &#39;,$convert_rule[&#39;to&#39;],"\n";
        }
        
    } else {
        echo "db error\n";
    }
}


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn