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 " "; } }

 

 

上一篇:Android 第一行代码学习记录


下一篇:697. 数组的度