利用PHP脚本辅助MySQL数据库管理5-检查异常数据

时间:2022-10-22 14:24:32
<?php
$dbi = new DbMysql;
$dbi->dbh = 'mysql://root:mysql@127.0.0.1/coffeetest'; $map = array();
$dbi->fetchMap("SHOW TABLES", $map);
$tables = array_keys($map); // 找出含有cid字段的表
$cid_tables = array();
for($j=0; $j<count($tables); $j++){
$map = array();
$dbi->fetchMap("DESCRIBE ".$tables[$j], $map);
foreach($map as $k=>$v){
if($k=='cid') {
$cid_tables[] = $tables[$j];
}
}
} // 找出cid=0的数据并修复
$cid = 1;
$cid0_tables = array();
for($j=0; $j<count($cid_tables); $j++){
$sql = "SELECT COUNT(*) FROM ".$cid_tables[$j]." WHERE cid = 0";
$count = $dbi->getval($sql, 0);
if($count>0) $cid0_tables[$cid_tables[$j]] = $count;
}
foreach($cid0_tables as $k=>$v){
echo("\t".$k."=".$v."\n");
$sql = "UPDATE ".$k." SET cid = ".$cid." WHERE cid = 0";
$dbi->execute($sql);
}
?>