Rumah >pembangunan bahagian belakang >tutorial php >php批量转换Mysql表引擎

php批量转换Mysql表引擎

巴扎黑
巴扎黑asal
2016-11-24 10:28:201196semak imbas

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


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP mysql 事务处理实例Artikel seterusnya:PHP操作Mysql存储过程