搜尋

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)!


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器