万能字典pdo连接数据库

Posted abner3721

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了万能字典pdo连接数据库相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Table</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
 $(function() {
        /* For zebra striping */
        // $("table tr:nth-child(odd)").addClass("odd-row");
        /* For cell text alignment */
        $("table td:first-child, table th:first-child").addClass("first");
        /* For removing the last border */
        $("table td:last-child, table th:last-child").addClass("last");
});
</script>

<style type="text/css">

    html, body, div, span, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    abbr, address, cite, code,
    del, dfn, em, img, ins, kbd, q, samp,
    small, strong, sub, sup, var,
    b, i,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td {
        margin:0;
        padding:0;
        border:0;
        outline:0;
        font-size:100%;
        vertical-align:baseline;
        background:transparent;
    }
    
    body {
        margin:0;
        padding:0;
        font:12px/15px "Helvetica Neue",Arial, Helvetica, sans-serif;
        color: #555;
        background:#f5f5f5 url(bg.jpg);
    }
    
    a {color:#666;}
    
    #content {width:65%; max-width:690px; margin:6% auto 0;}
    
    /*
    Pretty Table Styling
    CSS Tricks also has a nice writeup: http://css-tricks.com/feature-table-design/
    */
    
    table {
        overflow:hidden;
        border:1px solid #d3d3d3;
        background:#fefefe;
        width:70%;
        margin:5% auto 0;
        -moz-border-radius:5px; /* FF1+ */
        -webkit-border-radius:5px; /* Saf3-4 */
        border-radius:5px;
        -moz-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
        -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
    }
    
    th, td {padding:18px 28px 18px; text-align:center; }
    
    th {padding-top:22px; text-shadow: 1px 1px 1px #fff; background:#e8eaeb;}
    
    td {border-top:1px solid #e0e0e0; border-right:1px solid #e0e0e0;}
    
    tr.odd-row td {background:#f6f6f6;}
    
    td.first, th.first {text-align:left}
    
    td.last {border-right:none;}
    
    /*
    Background gradients are completely unnecessary but a neat effect.
    */
    
    td {
        background: -moz-linear-gradient(100% 25% 90deg, #fefefe, #f9f9f9);
        background: -webkit-gradient(linear, 0% 0%, 0% 25%, from(#f9f9f9), to(#fefefe));
    }
    
    tr.odd-row td {
        background: -moz-linear-gradient(100% 25% 90deg, #f6f6f6, #f1f1f1);
        background: -webkit-gradient(linear, 0% 0%, 0% 25%, from(#f1f1f1), to(#f6f6f6));
    }
    
    th {
        background: -moz-linear-gradient(100% 20% 90deg, #e8eaeb, #ededed);
        background: -webkit-gradient(linear, 0% 0%, 0% 20%, from(#ededed), to(#e8eaeb));
    }
    
    /*
    I know this is annoying, but we need additional styling so webkit will recognize rounded corners on background elements.
    Nice write up of this issue: http://www.onenaught.com/posts/266/css-inner-elements-breaking-border-radius
    
    And, since we‘ve applied the background colors to td/th element because of IE, Gecko browsers also need it.
    */
    
    tr:first-child th.first {
        -moz-border-radius-topleft:5px;
        -webkit-border-top-left-radius:5px; /* Saf3-4 */
    }
    
    tr:first-child th.last {
        -moz-border-radius-topright:5px;
        -webkit-border-top-right-radius:5px; /* Saf3-4 */
    }
    
    tr:last-child td.first {
        -moz-border-radius-bottomleft:5px;
        -webkit-border-bottom-left-radius:5px; /* Saf3-4 */
    }
    
    tr:last-child td.last {
        -moz-border-radius-bottomright:5px;
        -webkit-border-bottom-right-radius:5px; /* Saf3-4 */
    }

</style>

</head>
<body>

<div id="content">

    <table cellspacing="0">  
        <?php

            $table = ‘qbcms‘;
            $dsn = ‘mysql:host=127.0.0.1;dbname=‘.$table;
            $username = ‘root‘;
            $passwd = ‘root‘; 
            try{
                $opts_values = array(PDO::MYSQL_ATTR_INIT_COMMAND=>‘SET NAMES utf8‘);
                $pdo = new PDO($dsn,$username,$passwd,$opts_values); //2、new一个php数据对象PDO
            }catch(PDOException $e){
                echo $e->getMessage();
            }

            $sql2 = "SELECT TABLE_NAME 表名,  TABLE_COMMENT 表注释 FROM information_schema.TABLES WHERE table_schema=‘{$table}‘";
            // $sql2 = "SELECT * FROM qb_memberdata WHERE 1";
            // $result2 = $pdo->query($sql2,PDO::FETCH_ASSOC)->fetchAll();
            $rows = $pdo->query($sql2,PDO::FETCH_ASSOC)->fetchAll();

            foreach($rows as $k=>$v){
                $data[$v[‘表名‘]] = $v[‘表注释‘];
            }  
 

                $sql = "SELECT
                    TABLE_NAME 表名,
                       COLUMN_NAME 列名,
                    COLUMN_TYPE 数据类型,  
                      IS_NULLABLE 是否为空, 
                      COLUMN_COMMENT 备注
                FROM
                     INFORMATION_SCHEMA.COLUMNS
                where 
                    table_schema =‘{$table}‘";


                $rows2 = $pdo->query($sql,PDO::FETCH_ASSOC)->fetchAll();
                 
                $temp = ‘‘; 
                foreach($rows2  as $k=>$v ){

                    if($v[‘表名‘] != $temp){

                        $temp = $v[‘表名‘];
                         echo "<tr><td colspan=‘5‘></td></tr>";
                         echo "<tr><td colspan=‘5‘></td></tr>";
                         echo "<tr class=‘odd-row‘ ><td colspan=‘5‘ class=‘first last‘ >{$v[‘表名‘]}--{$data[$v[‘表名‘]]}</td></tr>";

                         // echo "<tr><td>表名</td><td>字段</td><td>数据类型</td> <td>默认值</td><td>备注</td></tr>";
                         echo "<tr>  <th>字段</th> <th>数据类型</th>   <th>默认值</th>  <th>备注</th> </tr>";
                    }

                    echo  "<tr>"; 
                     echo "<td>{$v[‘列名‘]}</td>";
                     echo "<td>{$v[‘数据类型‘]}</td>";
                     echo "<td>{$v[‘是否为空‘]}</td>"; 
                     echo "<td>{$v[‘备注‘]}</td>"; 
                     echo "</tr>";


                }
 



?> 
    </table>

</div>

</body>
</html>

 

以上是关于万能字典pdo连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

齐博x1 数据库字典--万能数据库字典代码

封装PDO连接数据库代码

利用PDO.so连接并读取Mysql数据库 核心代码实现讲解

php PHP:PDO片段

php PHP:PDO片段

object_PDO基础连接数据库代码