drkxgs9358 2015-02-24 01:05
浏览 31
已采纳

单击该表时,如何从数据库表列表中显示表的属性? [关闭]

<td>
                <p>
                <div class="column"> 
                    <p><b>SELECT A DATABASE</b></p>
                    <p> <?php echo$this->form->getInput('Database_1'); 
                    include "db1.js.php" ?>
                </div>
               </td>

               <td>
                <p>
                <div class="column" id="tbDiv">
                    <p><b>TABLES</b></p>
                  <select name="List of Tables" size="25" multiple id='table1' name='table1' title='List of Tables' class='inputbox'>
                  <option >Tables will be listed here...</option></select>
               </div><p> 
               </td>

This the code in displaying the tables in a list from a selected database. I want to display the attributes of the table when i click it.

  • 写回答

1条回答 默认 最新

  • dpwdsmbvm496180204 2015-02-24 04:05
    关注

    Try this using ajax and php

    <?php 
     $db = new PDO('mysql:host=localhost;dbname=mysql','root','');
     $dbs = $db->query( 'SHOW DATABASES' );
     ?>
    <!DOCTYPE html>
     <html>
     <head>
       <title></title>
    <style type="text/css">
    li:hover{
        cursor: pointer;
    }
    #list{
        float: left;
        width: 30%;
    }
    #table{
        float: right;
        width: 68%;
    }
    #main{
        width: 70%;
        margin: 0 auto;
    }
    table,th,td{
        border: 1px solid #000;
    }
    th{
        width: 120px;
        background-color: #000;
        color: #fff;
        text-transform: capitalize;
    }
    table {
    border-collapse: collapse;
    }
    </style>
    </head>
    <body>
     <div id="main">
    <div id="list">
    <select id="dbase" onchange="getTables(this.value)">
    <option>Select databse</option>
        <?php 
         while( ( $db = $dbs->fetchColumn( 0 ) ) !== false )
        {
        echo '<option>',$db,'</option>';
      }
         ?>
    </select>
    <!-- displaying dropdown ************* -->
     <option id="bulk" onchange="drop(this.value)"></option>
    </div>
    <div id="table"></div>
     </div>
      <script type="text/javascript">
      //*************update for displaying dropdown menu *******
        function drop(table){
    
      var table = table; 
      var db = dbase.value;
     var data = new XMLHttpRequest();
    data.open("POST","list.php");
    
    data.onreadystatechange = function(){
      if(data.readyState === 4 && data.status === 200){
        document.getElementById('table').innerHTML = data.responseText;
    
      }
    }
    data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    data.send('table='+table+'&dbase='+db);
    } 
    //***********end of update **********
     function getTables(table){
    var data = new XMLHttpRequest();
    data.open("POST","get_tables.php");
    
    data.onreadystatechange = function(){
      if(data.readyState === 4 && data.status === 200){
        document.getElementById('bulk').innerHTML = data.responseText;
    
      }
    }
    data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    data.send('table='+table);
     }
    </script>
    </body>
    </html>
    

    create get_tables.php

    <?php 
     $dbname = $_POST['table'];
     $db = new PDO("mysql:host=localhost;dbname=$dbname","root","");
     $query = $db->prepare('show tables');
     $query->execute();
     $tabe_in = 'Tables_in_'.$dbname;
     $results = $query->fetch(PDO::FETCH_ASSOC);
     while ($results != null) {
      // change <li> to <option> here **********
      echo '<option>',$results[$tabe_in],'</option>'; 
       $results = $query->fetch(PDO::FETCH_ASSOC);
     }
    
     ?>
    

    and res.php

    <?php 
     $table = $_POST['table'];
     $dbname = $_POST['dbase'];
     $sql = "SHOW COLUMNS FROM $table";
     $db = new PDO("mysql:host=localhost;dbname=$dbname","root","");
    
     $table_headers = array();
     $results = $db->prepare($sql);
     $results->execute();
     $res = $results->fetch();
    
     ?>
      <table>
      <tr>
     <?php 
      while ($res != null) {
       echo '<th>',$res['Field'],'</th>';
       $table_headers[] = $res['Field'];
      $res = $results->fetch();
      }
    
      ?>
       </tr>
    
     <?php 
     $query = $db->prepare("SELECT * FROM $table");
      $query->execute();
      $rs = $query->fetchAll();
    
     foreach ($rs as  $value) {
      echo '<tr>';
       foreach ($table_headers as $val) {
         echo '<td>',$value[$val],'</td>';
       }
       echo '</tr>';
     }
      ?>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求