1

So ive got a fully working page to display a dropdown list of all tables avaliable in my database. however i want it to display the list of all tables except one (as one of the tables contains user information which i dont want shown...) Is there a way to do this?

This is the section of the code ive got so far... $sql = "SHOW TABLES FROM $dbname"; $result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
if (mysql_select_db($dbname, $conn))
{
?>
    <form method="post" action="Browse.php">
    <select name="tables">
    <?php
    while ($row = mysql_fetch_row($result)) {
    ?>    
    <?php
        echo '<option value="'.$row[0].'">'.$row[0].'</option>';
    }
    ?>
    </select>
    <input type="submit" value="Show">
</form>
<?php
//mysql_free_result($result);
if (isset($_POST) && isset($_POST['tables']))
{
    $tbl=$_POST['tables'];
    //echo $_POST['tables']."<br />";
    $query="SELECT * from $tbl ORDER BY title ASC";
    $res=mysql_query($query);
    //echo $query;
    if ($res)
    {
    ?>
    <table border="1">
    <?php
        while ( $row = mysql_fetch_array($res))
        {
            echo "<tr>";
            //echo "<td>".$row[0]."</td>";
            echo "<td>".$row[1]."</td>";
            //echo "<td>".$row[2]."</td>";
            //echo "<td>".$row[3]."</td>";      
            echo "</tr>";
        } ?>
    </table>
    <?php
    }
}
}

Its no biggie if there isnt an easy way to do so as i've figured out that by doing the $query="SELECT * from $tbl ORDER BY title ASC"; it wont display the data (as there isnt a title column in the user details table)..but i dont want the table name to be shown in the dropdown box

Just a general query really... Thanks

3 Answers 3

5

You can use a where clause with the SHOW TABLES statement.

The trick is knowing the name of the column that's generated by the SHOW TABLES statement as its name depends on your database name. The name of the column will be "tables_in_{your_dbname}". So if your database name is "blah" the column name will be "tables_in_blah".

So if the table you want to omit from your result set is called "secret_table" you can execute the statement like so:

SHOW TABLES WHERE tables_in_blah <> 'secret_table';

Or if you want to use a wildcard you can do it like so:

SHOW TABLES WHERE tables_in_blah NOT LIKE '%secret_table%';
Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

SELECT table_name FROM information_schema.tables  
WHERE table_schema = 'your_data_base_name'  
AND table_name NOT LIKE '%USER_TABLE%'; 

Without like

SELECT table_name FROM information_schema.tables  
WHERE table_schema = 'your_data_base_name' AND table_name <> 'USER_TABLE'; 

Comments

2

Another alternative is to avoid outputting the table in question when you construct the list of options:

<?php
while ($row = mysql_fetch_row($result)) {
    if ($row[0] != 'my_private_table_name') {
        echo '<option value="'.$row[0].'">'.$row[0].'</option>';
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.