2012-07-18 285 views
0

我想創建一個頁面,我輸入一個MYSQL查詢:SELECT column1,column2 FROM table;,並動態創建一個帶有列標題的HTML表。從mysql查詢動態創建表

編輯:刪除了我的大部分問題,只是留下了問題的一部分。我從此創建了一個腳本來完成這個任務,並將其作爲答案。我希望有人像我一樣使用它(當我懶得登錄到phpmyadmin ...哈哈哈)。

+0

你打開SQL注入。 – 2012-07-19 00:33:51

+0

謝謝,這個例子用於概念使用 – 2012-07-20 23:26:46

回答

0

嘛。以下是我做到了這一點,建立在webjprgm的提示使用mysql_fetch_assoc:

php code to dynamically create a table with column titles, from PHP to mysql to html! :)

<head> 
    <title>mysql Table maker</title> 
</head> 
<body> 
    <center> 
<?php 

    /* posted data sent to this page (from this page)*/ 

    $pdatabase  = htmlentities($_POST['database'], ENT_QUOTES); // database 
    $phost   = htmlentities($_POST['host'],  ENT_QUOTES); // host 
    $puser   = htmlentities($_POST['user'],  ENT_QUOTES); // user 
    $ppassword  = htmlentities($_POST['password'], ENT_QUOTES); // password 

    $pcolumns  = htmlentities($_POST['columns'], ENT_QUOTES); // comma seperated columns 
    $columns  = explode(",",$pcolumns);       // array of column names 

    $ptable   = htmlentities($_POST['table'],  ENT_QUOTES); // table 
    $pwhere   = str_replace(array(";",'"'),array('',''),$_POST['where']); // WHERE clause 
    if (!empty($pwhere)) {$pwhere = "WHERE $pwhere";}     // if not empty, prepend with "WHERE" 

    $porder   = htmlentities($_POST['order'],  ENT_QUOTES); // ORDER BY clause 
    $psort   = htmlentities($_POST['sort'],  ENT_QUOTES); // SORTING (asc or desc) 
    if (!empty($porder)) {$porder = "ORDER BY $porder $psort";}   // if order is not empty, prepend with "ORDER BY" 

    $pgroup   = htmlentities($_POST['group'],  ENT_QUOTES); // GROUP BY clause 
    if (!empty($pgroup)) {$pgroup = "GROUP BY $pgroup";}    // if not empty, prepend with "GROUP BY" 

    $plimit   = htmlentities($_POST['limit'],  ENT_QUOTES); // LIMIT clause 
    if (!empty($plimit)) {$plimit = "LIMIT $plimit";} 

    /* The finished product....or query...so to speak...if you will */ 
    $query   = "SELECT $pcolumns FROM $ptable $pwhere $pgroup $porder $plimit"; 

    /* Safety precautions */ 
    $query   = str_replace(array("delete","drop","update","alter"),array('','','',''),$query); 


    // print_r($columns); 

?> 
<form action="mysql-table.php" method="POST"> 
    <span style="position:fixed;top:0;left:0;width:100%;height:30px;background:#35AFE3"> 
    host:  <input name="host"  type="text"  value="<?php echo $phost;?>"/> 
    user:  <input name="user"  type="text"  value="<?php echo $puser;?>"/> 
    password: <input name="password" type="password" value="<?php echo $ppassword;?>"/> 
    database: <input name="database" type="text"  value="<?php echo $pdatabase;?>"/> 
    </span> 

    <hr/> 

    <span style="position:fixed;top:30px;left:0;width:100%;height:205px;background:#35FC39;"> 
    SELECT 
     <input name="columns" type="text"  value="<?php echo $pcolumns;?>"/><br/> 
    FROM 
     <input name="table"  type="text"  value="<?php echo $ptable; ?>"/><br/> 
    WHERE 
     <input name="where"  type="text"  value="<?php echo trim(str_replace("WHERE","",$pwhere)); ?>"/><br/> 
    ORDER BY 
     <input name="order"  type="text"  value="<?php echo trim(str_replace("ORDER BY","",$porder)); ?>"/><br/>  
    SORT 
     <select name="sort"> 
      <option value=""></option> 
      <option value="ASC">ASC</option> 
      <option value="DESC">DESC</option> 
     </select><br/>  
    GROUP BY 
     <input name="group"  type="text"  value="<?php echo trim(str_replace("GROUP BY","",$pgroup)); ?>"/><br/> 
    LIMIT 
     <input name="limit"  type="text"  value="100"/><br/> 
    GO: 
     <input     type="submit" value="submit" /> 

    </span> 
</form> 



<span style="position:absolute;top:235px;left:0;width:100%;height:auto;background:#28c7d6;z-index:-1;"> 



<?php 
if (!empty($_POST['columns']) && !empty($_POST['table'])) { 

    echo "<h3><b>Query:</b> <i>$query</i></h3><hr/>"; 

    $mysqli = new mysqli($phost,$puser,$ppassword,$pdatabase);    // Connect to DB 

    /* check connection */            // check for connection error 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    if ($result = $mysqli->query($query)) { 


     echo "<table border='1' style='word-wrap:break-word'>";   // New table 
     /* Column Title */ 
     echo "<tr>";             // New Row for the titles 

     foreach($columns as $c) {          // For each column, create a column 
      echo "<td><b>$c</b></td>\r\n"; 
     } 

     echo "</tr>";             // Close the titles' row 


     /* DATA RESULTS */ 
     while ($row = $result->fetch_assoc()) {       // for each set of rows: 
      echo "<tr>\r\n\r\n";          // create new row 

      foreach($columns as $c) {         // for each column in the row: 
                     // create a cell 
       echo " 
        <td style='max-width:400px;'> 
         <div style='max-height:300px;overflow-y:auto;'> 
          $row[$c] 
         </div> 
        </td>"; 
      } 

      echo "</tr>";            // end of that row 

     }                // end foreach results 
     echo "</table>";            // closing of the table 

     /* free result set */ 
     $result->free(); 
    } else { 
     echo "query failed.<hr/>"; 
    } 
    /* close connection */ 
    $mysqli->close(); 
} elseif (isset($_POST['columns']) || isset($_POST['table'])) {   // end of !empty columns,table 
    echo "<b>MISSING IMPORTANT INFORMATION.<br/> 
    For column, you entered: <u> ".$_POST['columns']." </u><br/> 
    For table you entered: <u> ".$_POST['table']." </u>"; 
} 
?> 

</span> 
    </center> 
</body> 
2

我會嘗試mysql_fetch_assoc,它返回一個關聯數組(地圖)。然後你可以使用array_keys來獲取列名。

+1

對,然後使用foreach循環遍歷剩下的數據。 – 2012-07-18 23:25:31

+0

請停止使用已棄用的'mysql_'命令。 – 2012-07-19 00:33:08

+1

僅供參考,正確的新方法是'mysqli_fetch_assoc'。儘管我不得不在至少一個不支持mysqli_ *的服務器上發佈代碼,但那是在一年前。 「改進的mysql」方法更加安全,所以將它們用於任何新的應用程序。 – webjprgm 2012-07-19 04:41:13

1

這聽起來像是你有時只需要吐出結果看到他們在屏幕上它可能與測試,所以我要在這裏把這個...

PHP類DBUG是迅速獲得一個很好的格式化表格了一個PHP數組,甚至一個MySQL結果的真棒工具:

http://dbug.ospinto.com/