dounai6613 2015-05-02 11:26
浏览 43
已采纳

使用PHP将任何给定的MySQL SELECT查询显示为HTML表

Sample Query 1 -

SELECT ID,NAME FROM USERS

Sample Query 2 -

SELECT Orders.OrderID as ID, Customers.CustomerName as Name, Orders.OrderDate as Date
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID; 

How can I load title headings to an array in PHP? I mean ID,NAME in Query 1 and ID,Name,Date in Query 2

This is what I'm trying to do :

I'm creating a PHP function to make HTML table automatically from any given MySQL-Select query

This where I'm up to now

function createTable($query) {
    $sql_link = Connect_MySQLi_DB();// Database Connection
    $sql_link->set_charset("utf8");
    $result = $sql_link->query($query);
    $headings = array('ID','Name','Date');//I need this array to create automatically
    echo '<table>';
    echo '<tr>';
    for ($x = 0; $x <= (count($headings) - 1); $x++) {
        echo '<th>'.$headings[$x].'</th>';
    }
    echo '<tr>';
    while ($row = $result->fetch_object()) {
        echo '<tr>';
        for ($x = 0; $x <= (count($headings) - 1); $x++) {
            echo '<td>' . $row->$headings[$x] . '</td>';
        }
        echo '<tr>';
    }
    echo '</table>';
}

So I need to create that $heading array automatically. If I can do that, this function can display any MySQL-Select query as a HTML table

  • 写回答

2条回答 默认 最新

  • dousa2794 2015-05-03 01:06
    关注

    Finally this is how I made this function to work (Thanks Hynner for your excellent suggestion),

    function createTable_from_sql_select_query($query) {
        $sql_link = Connect_MySQLi_DB();// Database connection
        $sql_link->set_charset("utf8");
        $result = $sql_link->query($query);
    
        // Adding Missing array_column Function for Old PHP Versions (<5.5)
        if (!function_exists('array_column')) {
    
            function array_column($array, $column) {
                $ret = array();
                foreach ($array as $row)
                    $ret[] = $row[$column];
                return $ret;
            }
    
        }
    
        $headings = json_decode(json_encode($result->fetch_fields()), true);
        $headings = array_column($headings, 'name');
    
          $return = '<table>';
          $return .= '<thead><tr>';
          for ($x = 0; $x <= (count($headings) - 1); $x++) {
          $return .= '<th>' . ucwords(str_replace('_', ' ', (strtolower($headings[$x])))) . '</th>';
          }
          $return .= '</tr></thead><tbody>';
          while ($row = $result->fetch_object()) {
          $return .= '<tr>';
          for ($x = 0; $x <= (count($headings) - 1); $x++) {
          $return .= '<td>' . $row->$headings[$x] . '</td>';
          }
          $return .= '</tr>';
          }
          $return .= '</tbody></table>';
    
          return $return;
    }
    

    Now anyone can convert any SQL select query to a HTML table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题