4

I have created this function to get information about the tables, and their corresponding columns, in my MySQL database:

class SQL extends DB
{
    public static function showTables($alias='', $table_name='')
    {
        if(empty($alias) || $alias===true){  //  empty = show all tables from all available connections, true = show columns aswell
            foreach(self::get() as $con){  //  get() keeps, among other things, the aliases of all available connections
                $html .= '"'.$con['alias'].'": '.$con['db'];  //  the alias for the connection, and databasename, for the following table(s)
                $tables = self::con($con['alias'])->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);  //  fetch an array of all tables available through this connection
                foreach($tables as $table){
                    $html .= $table;  //  table name
                    if($alias===true){  //  show columns aswell when this is set to true
                        $columns = self::con($con['alias'])->query('SHOW COLUMNS FROM '.$table)->fetchAll(PDO::FETCH_COLUMN);  //  fetch an array of all column in this table
                        foreach($columns as $column){
                            $html .= $column;  //  column name
                        }
                    }
                }
            }
        } else {
            /*  basically the same code, but limits the result to a defined connection. And table if that is defined  */
        }
    }
}

It's important to know that this function is tailored to the way I set and use the PDO connections. Basically each connection has an alias, and the connection is accessed trough the given alias. But that has nothing to do with my issue.

Here's how I use the function, and what it produces:

<?=sql::showTables(true)?>

enter image description here
(I've removed all the css-styling from the code above)

This is ok. But I would also like to get, at least, the type, lenght and comment (if any).

I just tried, when I was writing this question, to put the getColumnMeta() inside the column foreach-loop, but that didn't work quite as expected:

    $columns = self::con($con['alias'])->query('SHOW COLUMNS FROM '.$table)->getColumnMeta(0);
    foreach($columns as $column){
        $meta = self::con($con['alias'])->query('SELECT `'.$column.'` FROM '.$table)->getColumnMeta(0);
        $html .= $meta['name'].' '.$meta['native_type'].'('.$meta['len'].')';
    }

enter image description here enter image description here

You can see the difference..
Does anyone know of another method for this? getting the type, lenght and comment.

Thanks in advance.

ThomasK
  • 2,210
  • 3
  • 26
  • 35

1 Answers1

7

If you want to select the comment column info on that particular table you can use:

SHOW FULL COLUMNS FROM table_test // with FULL option

Simple example:

$db = new PDO('mysql:host=localhost;dbname=DB_NAME;charset=utf8', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $db->query('SHOW FULL COLUMNS FROM table_test');
                      //  ^
$results = $query->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>';
print_r($results);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 1
    I actually never knew about the `FULL` option here and have always dug into information_schema for this stuff. Thanks! – Michael Berkowski Oct 02 '14 at 02:48
  • actually its weird, it cant seem to find that `FULL` option on the manual as well, i just saw it in a joomla forum while digging. normally i would just select stuff on information schema too – Kevin Oct 02 '14 at 03:00
  • @Fred-ii- actually i just search it because normally i don't go into select the comment too, so i'l just have to dig somewhere, lucky find – Kevin Oct 02 '14 at 03:02
  • 1
    Lucky find indeed! I found it here http://dev.mysql.com/doc/refman/5.0/en/show-columns.html - `SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]` - *"The `FULL` keyword causes the output to include the column collation and comments, as well as the privileges you have for each column."* – Funk Forty Niner Oct 02 '14 at 03:02
  • 1
    Searching/Researching has always been something I enjoyed doing for the longest time ;) Spending hours, even days at times is something I don't mind doing; **if** needed. – Funk Forty Niner Oct 02 '14 at 03:09
  • That was exactly what I needed. My searches didn't take me where I needed. – ThomasK Oct 02 '14 at 03:18
  • 1
    @ThomasK actually my first find wasn't this one, its on the information schema, i actually stumbled upon a joomla forum for this answer haha, but fred didn't even break a sweat in finding, as usual sharp as a tip of spear hehe, anyway im glad this answer helped – Kevin Oct 02 '14 at 03:22
  • Yeah. I noticed that based on the time it took before fred commented again. haha. My searches gave me the `getColumnMeta()`, so I tried a couple of different approaches of that with no luck. Anyway. So thanks for your digging :) – ThomasK Oct 02 '14 at 03:29
  • 2
    Google and I have had a mutual understanding/agreement for years now, *since its birth actually*. It gives me what I want and it sees me return, *whenever.* ;) I just don't click on their ads though. – Funk Forty Niner Oct 02 '14 at 03:32
  • @Fred-ii- That's why I use adblock – Mike Oct 02 '14 at 14:57