Using PHP script to assist MySQL database management 5-Check abnormal data

<?php
$dbi = new DbMysql;
$dbi->dbh = 'mysql://root:[email protected]/coffeetest';

$map = array();
$dbi->fetchMap("SHOW TABLES", $map);
$tables = array_keys($map);

// find out the table with cid field 
$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);
}
?>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325366288&siteId=291194637