2015年6月

批量将MyISAM引擎转换为InnoDB引擎

<?php
header('Content-type: text/html;charset=utf-8');
@error_reporting(E_ALL | E_STRICT);
@ini_set('display_errors', '1');

//填写数据库IP地址, 账号, 密码
$con = mysql_connect('127.0.0.1', 'root', 'root');
//填写需要转换的数据库名字
$dbName = 'minecraft_status';

$sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$dbName' AND ENGINE = 'MyISAM';";
$rs = mysql_query($sql, $con);

$count = 0;
$ok = 0;
while($row = mysql_fetch_array($rs))
{
    $count ++;
    $tbl = $row[0];
    $sql = "ALTER TABLE $dbName.$tbl ENGINE = INNODB;";
    $resultado = mysql_query($sql);
    if ($resultado){
        $ok ++;
        echo $sql."<hr/>";
    }
}
if ($count == $ok)
{
    echo '<div style="color: green"><b>全部表转换成功</b></div>';
}else{
    echo "<div style=\"color: red\"><b>出现错误</b>表的数量: $count, 成功转换的数量: $ok</div>";
}