Home >Backend Development >PHP Tutorial >PHP batch conversion Mysql table engine

PHP batch conversion Mysql table engine

巴扎黑
巴扎黑Original
2016-11-24 10:28:201198browse

<?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";
    }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn