Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude certain columns from SHOW COLUMNS mysql

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>";
like image 269
benhowdle89 Avatar asked Jan 21 '11 13:01

benhowdle89


People also ask

How do I show all columns except one in MySQL?

The information_schema. COLUMNS table holds all information about the columns in your MySQL tables. To exclude columns, you use the REPLACE() and GROUP_CONCAT() functions to generate the column names you wish to include in your SELECT statement later.

How do I display only certain columns in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.

How do I ignore a column in MySQL?

The syntax of the INSERT IGNORE statement is as follows: INSERT IGNORE INTO table(column_list) VALUES( value_list), ( value_list), ... Note that the IGNORE clause is an extension of MySQL to the SQL standard.


1 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');
like image 144
shamittomar Avatar answered Oct 21 '22 12:10

shamittomar