search
Homephp教程PHP源码php mysql query

php mysql query

May 26, 2016 am 08:18 AM

php mysql  query

<?php
/**
 * 生成mysql数据字典
 */
header("Content-type: text/html; charset=utf-8");
//配置数据库
$dbserver   = "localhost";
$dbusername = "root";
$dbpassword = "";
$database   = "fw";
 
//其他配置
$mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
mysql_select_db($database, $mysql_conn);
mysql_query(&#39;SET NAMES UTF8&#39;, $mysql_conn);
$table_result = mysql_query(&#39;show tables&#39;, $mysql_conn);
 
$no_show_table = array();    //不需要显示的表
$no_show_field = array();   //不需要显示的字段
 
//取得所有的表名
while($row = mysql_fetch_array($table_result)){
    if(!in_array($row[0],$no_show_table)){
        $tables[][&#39;TABLE_NAME&#39;] = $row[0];
    }
}
//替换所以表的表前缀
if($_GET[&#39;prefix&#39;]){
    $prefix = &#39;ych&#39;;
    foreach($tables as $key => $val){
        $tableName = $val[&#39;TABLE_NAME&#39;];
        $string = explode(&#39;_&#39;,$tableName);
        if($string[0] != $prefix){  
            $string[0] = $prefix;  
            $newTableName = implode(&#39;_&#39;, $string);  
            mysql_query(&#39;rename table &#39;.$tableName.&#39; TO &#39;.$newTableName);  
        }
    }
    echo "替换成功!";exit();
}
 
//循环取得所有表的备注及表中列消息
foreach ($tables as $k=>$v) {
    $sql  = &#39;SELECT * FROM &#39;;
    $sql .= &#39;INFORMATION_SCHEMA.TABLES &#39;;
    $sql .= &#39;WHERE &#39;;
    $sql .= "table_name = &#39;{$v[&#39;TABLE_NAME&#39;]}&#39;  AND table_schema = &#39;{$database}&#39;";
    $table_result = mysql_query($sql, $mysql_conn);
    while ($t = mysql_fetch_array($table_result) ) {
        $tables[$k][&#39;TABLE_COMMENT&#39;] = $t[&#39;TABLE_COMMENT&#39;];
    }
 
    $sql  = &#39;SELECT * FROM &#39;;
    $sql .= &#39;INFORMATION_SCHEMA.COLUMNS &#39;;
    $sql .= &#39;WHERE &#39;;
    $sql .= "table_name = &#39;{$v[&#39;TABLE_NAME&#39;]}&#39; AND table_schema = &#39;{$database}&#39;";
 
    $fields = array();
    $field_result = mysql_query($sql, $mysql_conn);
    while ($t = mysql_fetch_array($field_result) ) {
        $fields[] = $t;
    }
    $tables[$k][&#39;COLUMN&#39;] = $fields;
}
mysql_close($mysql_conn);
 
 
$html = &#39;&#39;;
//循环所有表
foreach ($tables as $k=>$v) {
    $html .= &#39;  <h3>&#39; . ($k + 1) . &#39;、&#39; . $v[&#39;TABLE_COMMENT&#39;] .&#39;  (&#39;. $v[&#39;TABLE_NAME&#39;]. &#39;)</h3>&#39;."\n";
    $html .= &#39;  <table border="1" cellspacing="0" cellpadding="0" width="100%">&#39;."\n";
    $html .= &#39;      <tbody>&#39;."\n";
    $html .= &#39;          <tr>&#39;."\n";
    $html .= &#39;              <th>字段名</th>&#39;."\n";
    $html .= &#39;              <th>数据类型</th>&#39;."\n";
    $html .= &#39;              <th>默认值</th>&#39;."\n";
    $html .= &#39;              <th>允许非空</th>&#39;."\n";
    $html .= &#39;              <th>自动递增</th>&#39;."\n";
    $html .= &#39;              <th>备注</th>&#39;."\n";
    $html .= &#39;          </tr>&#39;."\n";
 
    foreach ($v[&#39;COLUMN&#39;] as $f) {
        if(!is_array($no_show_field[$v[&#39;TABLE_NAME&#39;]])){
            $no_show_field[$v[&#39;TABLE_NAME&#39;]] = array();
        }
        if(!in_array($f[&#39;COLUMN_NAME&#39;],$no_show_field[$v[&#39;TABLE_NAME&#39;]])){
            $html .= &#39;          <tr>&#39;."\n";
            $html .= &#39;              <td class="c1">&#39; . $f[&#39;COLUMN_NAME&#39;] . &#39;</td>&#39;."\n";
            $html .= &#39;              <td class="c2">&#39; . $f[&#39;COLUMN_TYPE&#39;] . &#39;</td>&#39;."\n";
            $html .= &#39;              <td class="c3">&#39; . $f[&#39;COLUMN_DEFAULT&#39;] . &#39;</td>&#39;."\n";
            $html .= &#39;              <td class="c4">&#39; . $f[&#39;IS_NULLABLE&#39;] . &#39;</td>&#39;."\n";
            $html .= &#39;        <td class="c5">&#39; . ($f[&#39;EXTRA&#39;]==&#39;auto_increment&#39;?&#39;是&#39;:&#39; &#39;) . &#39;</td>&#39;."\n";
            $html .= &#39;              <td class="c6">&#39; . $f[&#39;COLUMN_COMMENT&#39;] . &#39;</td>&#39;."\n";
            $html .= &#39;          </tr>&#39;."\n";
        }
    }
    $html .= &#39;      </tbody>&#39;."\n";
    $html .= &#39;  </table>&#39;."\n";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>数据库数据字典</title>
<style>
body, td, th { font-family: "微软雅黑"; font-size: 14px; }
.warp{margin:auto; width:900px;}
.warp h3{margin:0px; padding:0px; line-height:30px; margin-top:10px;}
table { border-collapse: collapse; border: 1px solid #CCC; background: #efefef; }
table th { text-align: left; font-weight: bold; height: 26px; line-height: 26px; 
font-size: 14px; text-align:center; border: 1px solid #CCC; padding:5px;}
table td { height: 20px; font-size: 14px; border: 1px solid #CCC; background-color: #fff; padding:5px;}
.c1 { width: 120px; }
.c2 { width: 120px; }
.c3 { width: 150px; }
.c4 { width: 80px; text-align:center;}
.c5 { width: 80px; text-align:center;}
.c6 { width: 270px; }
</style>
</head>
<body>
<p class="warp">
<?php echo $html; ?>
</p>
</body>
</html>

 以上就是php ,mysql _query的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment