Home » Php » php – Exclude certain columns from SHOW COLUMNS mysql

php – Exclude certain columns from SHOW COLUMNS mysql

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have this query: SHOW COLUMNS FROM mash which works fine in my while loop for building a select element made from table column names. But in my table i have “id” and “tstamp” which i dont want in the select element, is this possible to exclude these columns?

echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
            connectDB();
            $result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
            echo '<select name="column" class="column">';
            while ($row = mysql_fetch_array($result)) {
                echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
            }
            closeConn();
            echo '</select>';
echo "</form>";
How to&Answers:

PHP Way:

Use a continue in the while loop, when those fields are fetched, like this:

echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
    connectDB();
    $result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
    echo '<select name="column" class="column">';
    while ($row = mysql_fetch_array($result))
    {
        if($row[0] == 'id' || $row[0] == 'tstamp')
              continue;
        echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
    }
    closeConn();
    echo '</select>';
echo "</form>";

This will just skip the id and tstamp fields and process all others. continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.


MySQL Way:

Remove those fields in the query like this:

SHOW COLUMNS FROM mash WHERE Field NOT IN ('id', 'tstamp');

Answer:

Yes, it’s possible. Instead of using SHOW COLUMNS use INFORMATION_SCHEMA.

INFORMATION_SCHEMA is the ANSI way of extracting metadata from a relational database. In MySQL you can use:

$sql = "select column_name from information_schema.columns c " + 
       " where c.table_schema = 'db_name' " + 
       "   and c.table_name='table_name' " + 
       "   and c.column_name like 'name%'";

Information Schema has the advantage that is SQLANSI compliant. You can use it in MySQL, PostgreSQL, SQLServer and other relational databases.

Answer:

You can use LIKE operator.

SHOW COLUMNS FROM mash LIKE "name%"

Answer:

To see just the “Collation” column that shows up in SHOW FULL COLUMNS from tablename for just one field, it’s:

select COLLATION_NAME from information_schema.columns
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';

Ddrop the fieldname in the where, if you want to see that column for all field names.

To see all possible column names that show up from SHOW FULL COLUMNS so that you can select what you want:

select * from information_schema.columns 
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';