ThinkPHP5 数据字典,直接给数据库连接信息即可 不依赖框架.

   /**
    * PHP 数据字典,直接给数据库连接信息即可 不依赖框架.
    * 可以使用锚点快速定位表
    * User: mybook-lhp
    * Date: 18/6/20
    * Time: 下午2:52
    */

   namespace app\common\utils;

   use \PDO;
   use \PDOException;

   class UtilDbdic
   {
      protected        $html_tpl;
      protected        $table_tpl;
      protected        $table_data;
      static protected $Init = null;

      static public function Init()
      {
         if (static::$Init == null)
         {
            static::$Init = new static();
         }
         return static::$Init;
      }

      static public function flash($DbConfig)
      {
         return static::Init()->export_dict($DbConfig)->table()->html();
      }

      /**
       * @param $config ['database']
       * @param $config
       * @return $this
       */
      public function export_dict($config)
      {
         $dsn = 'mysql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
         //数据库连接
         try
         {
            $con = new PDO($dsn, $config['username'], $config['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
         } catch (PDOException $e)
         {
            die('Connection failed: ' . $e->getMessage());
         }


         $tables = $con->query('SHOW tables')->fetchAll(PDO::FETCH_COLUMN);

         //取得所有的表名
         foreach ($tables as $table)
         {
            $this->table_data[]['TABLE_NAME'] = $table;
         }

         //循环取得所有表的备注及表中列消息
         foreach ($this->table_data as $k => $v)
         {

            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.TABLES ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$config['database']}'";
            $tr = $con->query($sql)->fetch(PDO::FETCH_ASSOC);
            $this->table_data[$k]['TABLE_COMMENT'] = $tr['TABLE_COMMENT'];

            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$config['database']}'";
            $fields = [];
            $field_result = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC);

            foreach ($field_result as $fr)
            {
               $fields[] = $fr;
            }
            $this->table_data[$k]['COLUMN'] = $fields;
         }
         unset($con);
         return $this;

      }

      /**
       * @return $this
       */
      public function table()
      {
         $this->table_tpl = '';

         //循环所有表
         foreach ($this->table_data as $k => $v)
         {
            $this->table_tpl .= '.$v['TABLE_NAME'].'">';
            $this->table_tpl .= '';

            $this->table_tpl .= '';
            $this->table_tpl .= '
'
. PHP_EOL; $this->table_tpl .= ''; foreach ($v['COLUMN'] as $f) { $this->table_tpl .= ''; $this->table_tpl .= '' . PHP_EOL; $this->table_tpl .= ''; } $this->table_tpl .= '
' . $v['TABLE_NAME'] . $v['TABLE_COMMENT'] . '
字段名 数据类型默认值允许非空自动递增备注
' . $f['COLUMN_NAME'] . '' . $f['COLUMN_TYPE'] . '' . $f['COLUMN_DEFAULT'] . '' . $f['IS_NULLABLE'] . '' . ($f['EXTRA'] == 'auto_increment' ? '是' : '') . '' . (empty($f['COLUMN_COMMENT']) ? '-' : str_replace('|', '/', $f['COLUMN_COMMENT'])) . '
'
; } return $this; } /** * @param $table * @return string */ public function html() { return << Beautiful design tables in HTML in the style of a zebra.
{$this->table_tpl}
EOC; } }

你可能感兴趣的:(php代码段,分享,原创)