Question: Many people will think this is a duplicate question and answers to such question is already given.But I have a different problem, here I don't know the number of columns and name of columns!

I have a text input type in html in which a user can directly manipulate database. User can query any table in database. All tables have different column names and number of columns. And i can't use 'describe' and 'show column' sql statement since name of the column is unknown.

All the answers to this question considers programmer already know column name
and number of columns in table.

So the question is:

1. how to get number of columns in table?
2. how to get column names of table to display it in table heading tag?

Answer: 
You can use `DESC TABLE_NAME`.

Itere the return to know the amount of fields.
So i got the answer of my question from php documentation! Here i will post snippet of my code which executes 'select' SQL statement written run-time which shows header plus records of table from result i got from executing query. 

```
      if(strcmp($words[0], "select")==0) //for making sure its select statement
    {
        $result= mysqli_query($conn, $sql);
        if($result)
        {
            echo '<table border=1 style="border-collaspe=collaspe" class="table table-striped table-bordered table-hover dataTable no-footer">';
            echo '<tr>';
            for($i=0;$i<mysqli_num_fields($result);$i++)
            {
                $column_info=mysqli_fetch_field_direct($result, $i); 
               /*this function returns all the information about table metioned in the query.
               variable i over here refers to field no*/
                echo "<th>".$column_info->name."</th>"; //here fetching only name from whole information
            }
            echo '</tr>';
            while ($row = mysqli_fetch_array($result))
            {
                echo '<tr>';
                for($i=0;$i<mysqli_num_fields($result);$i++)
                {
                    echo '<td>'.$row[$i].'</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
        }
        else
        {
            echo "<script>alert('Error occured, Please check your syntax or internet connection')</script>";
        }
    }

```

For detailed information:
[PHP Documention page for mysqli\_fetch\_field\_direct ( mysqli\_result $result , int $fieldnr ) function](http://php.net/manual/en/mysqli-result.fetch-field-direct.php)