php代码:
<?php class tools{ /** * * @param string $usr 用户名 * @param string $pwd 密码 * @param string $dbname 数据库名称 * @param string $charset 字符集 */ function repair_mysql_tables($usr,$pwd,$dbname,$charset="utf8"){ $dsn="mysql:host=127.0.0.1;port=3306;dbname=".$dbname; try{ $conn=new PDO($dsn ,$usr,$pwd,array(PDO::ATTR_PERSISTENT=>true)); $conn->exec("SET NANMES".$charset); }catch(PDOException $e){ echo "Error!:".$e->getMessage()."<br/>"; die('db connect failed ...'); } $arr=$conn->query("show tables"); $arr1=$arr->fetchAll(PDO::FETCH_ASSOC); foreach ($arr1 as $a){ foreach ($a as $a1){ $conn->query("repair table ".$a1); echo "table :<span style='color:red;'>".$a1."</span> is repair..<br/>"; } } echo "task complete !"; } } //调用方法 $too=new tools(); $too->repair_mysql_tables("root", "", "fangjia"); ?>