PHP数据库批量去注释、删字段
作者:互联网
<?php $host = "localhost"; $username = 'root'; $password = 'root'; $database = 'dbname'; $filters = ['content','payload']; $pdo = new PDO("mysql:host={$host};dbname={$database}", $username, $password, array(PDO::ATTR_PERSISTENT => true)); $pdo->exec("set names 'utf8'"); $info = $pdo->query('SHOW TABLE STATUS;')->fetchAll(PDO::FETCH_ASSOC); foreach ($info as $key => $value) {
// 删除所有表注释 $pdo->exec("ALTER TABLE " . $value['Name'] . " COMMENT '';");
$info = $pdo->query("select * from information_schema.columns where table_name = '" . $value['Name'] . "'")->fetchAll(PDO::FETCH_ASSOC); foreach ($info as $i => $ivalue) {
// 删除整个数据库所有表中,存在在$filters数组中的列 if (in_array($ivalue['COLUMN_NAME'], $filters)) { $pdo->exec("ALTER TABLE " . $value['Name'] . " DROP COLUMN " . $ivalue['COLUMN_NAME'] . ";"); echo "删除表" . $value['Name'] . "中的:" . $ivalue['COLUMN_NAME']; echo " "; }
// 删除所有表的所有字段的注释 $pdo->exec("ALTER TABLE " . $value['Name'] . " MODIFY COLUMN " . $ivalue['COLUMN_NAME'] . " " . $ivalue['COLUMN_TYPE'] . " COMMENT '';"); echo "删除表" . $value['Name'] . "中的:" . $ivalue['COLUMN_NAME'] . "字段的注释"; echo " "; } }
标签:Name,COLUMN,数据库,value,pdo,ivalue,删字,PHP,NAME 来源: https://www.cnblogs.com/dwj192/p/15303050.html