php导出字典-成都快上网建站

php导出字典

public  function promotion_list()
    {
        $sql =     "CREATE TABLE `ka_sys` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
  `sys_name` varchar(45) CHARACTER SET utf8 NOT NULL COMMENT '系统中文名称',
  `eng_name` varchar(65) DEFAULT NULL,
  `sys_logo1` varchar(65) CHARACTER SET utf8 DEFAULT NULL COMMENT '大图',
  `sys_logo2` varchar(65) CHARACTER SET utf8 DEFAULT NULL COMMENT '小图',
  `sys_num` varchar(60) DEFAULT NULL,
  `created` int(11) NOT NULL COMMENT '创建时间',
  `updated` int(11) NOT NULL COMMENT '更新时间',
  `deleted` enum('1','0') NOT NULL DEFAULT '0' COMMENT '冻结',
  `parent_id` int(11) DEFAULT '0' COMMENT '系统父ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=latin1";
        preg_match_all("/(`.+`)(.+)('.+'),/i",$sql,$out);
        echo '

';
        print_r($out);
    }//
    
    public function create_database_doc()
    {
       //  /*
          header("Content-Type:   application/msword;charset=gbk");       
          header("Content-Disposition:   attachment;   filename=doc.doc");       
          header("Pragma:   no-cache");       
          header("Expires:   0");       
       
        
        // */
          
          $sql_query = "show tables;";
         
          $query     = $this->db->query($sql_query);
          $rowset    = $query->result_array();
          $tablearrs    = array();
          foreach($rowset as $key => $value)
          {
              $sql_query = "desc ". $value['Tables_in_ka'];
           //   echo $sql_query;
              $query     = $this->db->query($sql_query);
              $reset     = $query->result_array();
              $this->get_notes($value['Tables_in_ka'], $reset);
              $tablearrs[$key]['tab_name'] = $value['Tables_in_ka'];
              $tablearrs[$key]['feild_info'] = $reset;
          }
          $output ='';
          foreach ($tablearrs as $key => $value)
          {
                 $output   .=   '表'.$tablearrs[$key]['tab_name'].PHP_EOL;
                $output    .=   '';    
                $output   .=   ''
                            . ''
                            . ''
                            . ''
                            . ''
                            . ''
                            . '';   
                foreach ($tablearrs[$key]['feild_info'] as $ky => $val)
                {
                      $output   .=   ''
                            . ''
                            . ''
                            . ''
                            . ''
                            . ''
                            . '';
                }
                
               
                $output   .=   '
FieldTypeNullKeyDefaultExtranotes
'.$val['Field'].''.$val['Type'].''.$val['Null'].''.$val['Key'].''.$val['Default'].''.$val['Extra'].''.$val['notes'].'
'.PHP_EOL;
          }
         //   $output    =   mb_convert_encoding($output, "GBK","UTF-8");
          echo   $output;       
 
    }//end func create_database_doc
    
    public function get_notes($table_name,&$reset)
    {
         $sql_query = "show create table ".$table_name;
         //echo $sql_query;
         $query     = $this->db->query($sql_query);
         $rowset    = $query->result_array();
         $sql       = $rowset[0]['Create Table'];
         preg_match_all("/`(.+)`(.+)['(.*)'],/i",$sql,$out);
     
         
         foreach ($reset as $key => $val)
         {                   
              @preg_match_all("/^(.*)\'([^\']*)/i",$out[2][$key],$t_out);
              if( count($reset)-count($out[2])<=2)
              {
                  if(strpos($reset[$key]['Field'], 'id')!==FALSE  && count($reset)-count($out[2])==1)
                       @preg_match_all("/^(.*)\'([^\']*)/i",$out[2][$key-1],$t_out);
                      @$reset[$key]['notes']  =$t_out[2][0];
              }
              else
              {
                    @$reset[$key]['notes']  ='';
              }
              
              
         }
      
         
         
    }//end func get_notes

我们提供的服务有:成都网站设计、成都网站制作、微信公众号开发、网站优化、网站认证、合作ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的合作网站制作公司

//----------------------------------------------------------------------------------------

/**
 * 生成MySQL数据字典
 */
header ( "Content-type: text/html; charset=utf-8" );
 
// 配置数据库
$dbserver = "localhost";
$dbusername = "root";
$dbpassword = "123";
$database = "db153";
 
// 其他配置
$title = '数据字典';
 
$mysql_conn = @mysql_connect ( "$dbserver", "$dbusername", "$dbpassword" ) or die ( "Mysql connect is error." );
mysql_select_db ( $database, $mysql_conn );
mysql_query ( 'SET NAMES utf8', $mysql_conn );
$table_result = mysql_query ( 'show tables', $mysql_conn );
// 取得所有的表名
while ( $row = mysql_fetch_array ( $table_result ) ) {
    $tables [] ['TABLE_NAME'] = $row [0];
}
 
// 循环取得所有表的备注及表中列消息
foreach ( $tables as $k => $v ) {
    $sql = 'SELECT * FROM ';
    $sql .= 'INFORMATION_SCHEMA.TABLES ';
    $sql .= 'WHERE ';
    $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
    //echo $sql.'
';
    $table_result = mysql_query ( $sql, $mysql_conn );
    while ( $t = mysql_fetch_array ( $table_result ) ) {
        $tables [$k] ['TABLE_COMMENT'] = $t ['TABLE_COMMENT'];
    }
     
    $sql = 'SELECT * FROM ';
    $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
    $sql .= 'WHERE ';
    $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
     
    $fields = array ();
    //echo $sql.'
';
    $field_result = mysql_query ( $sql, $mysql_conn );
    while ( $t = mysql_fetch_array ( $field_result ) ) {
        $fields [] = $t;
    }
    $tables [$k] ['COLUMN'] = $fields;
}
mysql_close ( $mysql_conn );
 
$html = '';
// 循环所有表
foreach ( $tables as $k => $v ) {
    // $html .= '

'. $v['TABLE_COMMENT'] . ' 

';
    $html .= '';
    $html .= '' . $v ['TABLE_NAME'] . '  ' . $v ['TABLE_COMMENT'] . '';
    $html .= '字段名数据类型默认值
    允许非空
    自动递增备注';
    $html .= '';
     
    foreach ( $v ['COLUMN'] as $f ) {
        $html .= '' . $f ['COLUMN_NAME'] . '';
        $html .= '' . $f ['COLUMN_TYPE'] . '';
        $html .= ' ' . $f ['COLUMN_DEFAULT'] . '';
        $html .= ' ' . $f ['IS_NULLABLE'] . '';
        $html .= '' . ($f ['EXTRA'] == 'auto_increment' ? '是' : ' ') . '';
        $html .= ' ' . $f ['COLUMN_COMMENT'] . '';
        $html .= '';
    }
    $html .= '

';
}
 
// 输出
echo '


' . $title . '


';
echo '

' . $title . '

';
echo $html;
echo '';
 
?>

mysqli

/**

 * 生成mysql数据字典

 */

header ( "Content-type: text/html; charset=utf-8" );

 

// 配置数据库

$dbserver = "192.168.5.220";

$dbusername = "root";

$dbpassword = "root";

$database = "card";

 

// 其他配置

$title = '数据字典';

 

$mysql_conn = new mysqli( "$dbserver", "$dbusername", "$dbpassword","$database" ) ; 

$mysql_conn->query ( 'SET NAMES utf8'  );

$table_result = $mysql_conn->query  ( 'show tables' );

// 取得所有的表名

while ( $row = $table_result->fetch_array (  ) ) {

    $tables [] ['TABLE_NAME'] = $row [0];

}

 

// 循环取得所有表的备注及表中列消息

foreach ( $tables as $k => $v ) {

    $sql = 'SELECT * FROM ';

    $sql .= 'INFORMATION_SCHEMA.TABLES ';

    $sql .= 'WHERE ';

    $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";

    //echo $sql.'
';

    $table_result = $mysql_conn->query ( $sql  );

    while ( $t = $table_result->fetch_array (  ) ) {

        $tables [$k] ['TABLE_COMMENT'] = $t ['TABLE_COMMENT'];

    }

     

    $sql = 'SELECT * FROM ';

    $sql .= 'INFORMATION_SCHEMA.COLUMNS ';

    $sql .= 'WHERE ';

    $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";

     

    $fields = array ();

    //echo $sql.'
';

    $field_result = $mysql_conn->query ( $sql );

    while ( $t = $field_result->fetch_array (  ) ) {

        $fields [] = $t;

    }

    $tables [$k] ['COLUMN'] = $fields;

}

  $mysql_conn->close();;

 

$html = '';

// 循环所有表

foreach ( $tables as $k => $v ) {

    // $html .= '

'. $v['TABLE_COMMENT'] . ' 

';

    $html .= '';

    $html .= '' . $v ['TABLE_NAME'] . '  ' . $v ['TABLE_COMMENT'] . '';

    $html .= '字段名数据类型默认值

    允许非空

    自动递增备注';

    $html .= '';

     

    foreach ( $v ['COLUMN'] as $f ) {

        $html .= '' . $f ['COLUMN_NAME'] . '';

        $html .= '' . $f ['COLUMN_TYPE'] . '';

        $html .= ' ' . $f ['COLUMN_DEFAULT'] . '';

        $html .= ' ' . $f ['IS_NULLABLE'] . '';

        $html .= '' . ($f ['EXTRA'] == 'auto_increment' ? '是' : ' ') . '';

        $html .= ' ' . $f ['COLUMN_COMMENT'] . '';

        $html .= '';

    }

    $html .= '

';

}

 

// 输出

echo '

' . $title . '

';

echo '

' . $title . '

';

echo $html;

echo '';

 

?>


当前名称:php导出字典
文章链接:http://kswjz.com/article/gcspse.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流