Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:18:41+00:00 2026-06-08T18:18:41+00:00

I’m trying to create a page where I input a MYSQL query: SELECT column1,column2

  • 0

I’m trying to create a page where I input a MYSQL query: SELECT column1,column2 FROM table;, and dynamically create an HTML table with column titles.

EDIT: removed most of my question, just left the question part. I have since created a script that does this and included it as an answer. I hope someone else makes use of it like I do (when I’m too lazy to log into phpmyadmin … hahaha).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:18:42+00:00Added an answer on June 8, 2026 at 6:18 pm

    Well. Here’s how I accomplished this, building on webjprgm’s tip to use 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.