Display any given MySQL SELECT query as a HTML table using PHP

M_R_K

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

M_R_K

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using PHP to display MySQL in HTML

From Dev

MYSQL > Using a query as a table

From Dev

MySQL query displays no results in HTML table with PHP

From Dev

confusing with mysql Nested Select Query using php

From Dev

how to display mysql query output in HTML Horizontal table

From Dev

How to display records of select query with left join in table using foreach?

From Dev

short php/mysql query to delete where two table select

From Dev

Data display on PHP (Select Query)

From Dev

How to display multiple result from MySql query using PHP error

From Dev

PHP MySQL: Display all column names from a given table and the values for any single given row

From Dev

How to display multiple result from MySql query using PHP

From Dev

Display MySQL query in a HTML table

From Dev

Display data from mysql query in table format php

From Dev

PHP How to display mysql select query to table

From Dev

MySQL query entire table using 1 of 2 given matching parameters in first query

From Dev

Display any given MySQL SELECT query as a HTML table using PHP

From Dev

How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

From Dev

How to display database value inside select tag using PHP and MySQL

From Dev

Display Time in select query - Mysql

From Dev

short php/mysql query to delete where two table select

From Dev

display mysql query results in xml format using php

From Dev

MYSQL select query in PHP

From Dev

php in html - display table

From Dev

Change value of html table column, generated in MySQL query, using php

From Dev

Adding elements from a MYSQL query to a HTML Table using PHP

From Dev

php code to retrieve data from mysql database and display in html table

From Dev

how to display output of PHP MySQL query using Angular

From Dev

How to fix the display of the table using PHP and html

From Dev

select query inside in select query php mysql

Related Related

  1. 1

    Using PHP to display MySQL in HTML

  2. 2

    MYSQL > Using a query as a table

  3. 3

    MySQL query displays no results in HTML table with PHP

  4. 4

    confusing with mysql Nested Select Query using php

  5. 5

    how to display mysql query output in HTML Horizontal table

  6. 6

    How to display records of select query with left join in table using foreach?

  7. 7

    short php/mysql query to delete where two table select

  8. 8

    Data display on PHP (Select Query)

  9. 9

    How to display multiple result from MySql query using PHP error

  10. 10

    PHP MySQL: Display all column names from a given table and the values for any single given row

  11. 11

    How to display multiple result from MySql query using PHP

  12. 12

    Display MySQL query in a HTML table

  13. 13

    Display data from mysql query in table format php

  14. 14

    PHP How to display mysql select query to table

  15. 15

    MySQL query entire table using 1 of 2 given matching parameters in first query

  16. 16

    Display any given MySQL SELECT query as a HTML table using PHP

  17. 17

    How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

  18. 18

    How to display database value inside select tag using PHP and MySQL

  19. 19

    Display Time in select query - Mysql

  20. 20

    short php/mysql query to delete where two table select

  21. 21

    display mysql query results in xml format using php

  22. 22

    MYSQL select query in PHP

  23. 23

    php in html - display table

  24. 24

    Change value of html table column, generated in MySQL query, using php

  25. 25

    Adding elements from a MYSQL query to a HTML Table using PHP

  26. 26

    php code to retrieve data from mysql database and display in html table

  27. 27

    how to display output of PHP MySQL query using Angular

  28. 28

    How to fix the display of the table using PHP and html

  29. 29

    select query inside in select query php mysql

HotTag

Archive