GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Apr-2009, 11:50
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

PHP/MySQL Explain Table Script


This script prints out a table similar to what you would see if you ran an explain query using the MySQL command-line interface (minus the borders). Simply set the defined constants to match your database and run the script in your browser. You can change the column widths in the $widths array on line 14.

explain_table.php
PHP Code:

<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'database');
define('DB_TABLE', 'table');

$db = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to MySQL server.');
mysql_select_db(DB_NAME, $db) or die('Could not connect to MySQL database.');

if ($result = mysql_query('EXPLAIN `'.DB_TABLE.'`', $db))
{
    echo '<pre>';
    $widths = array // Column names and widths (number of spaces)
    (    'Field'   => 20
    ,    'Type'    => 12
    ,    'Null'    => 4
    ,    'Key'     => 3
    ,    'Default' => 7
    ,    'Extra'   => 14
    );
    $margin = 2; // Number of spaces between columns
    // Displays column headings
    foreach ($widths as $field => $width)
    {
        /*
         * Limits the width of the column. If the contents are wider than the
         * column, the full contents can be displayed by hovering the mouse
         * over the cell
         */
        echo '<span title="'.$field.'">';
        if (strlen($field) > $width)
            $field = substr($field, 0, $width - 1).'…';
        echo '<b>'.str_pad($field, $width + $margin, ' ', STR_PAD_RIGHT).'</b></span>';
    }
    echo "\n";
    // Displays a row for each explained field containing name, type, etc.
    while ($row = mysql_fetch_assoc($result))
    {
        // Displays each column for the current row
        foreach ($widths as $field => $width)
        {
            // Similar to the previous foreach loop
            echo '<span title="'.$row[$field].'">';
            if (strlen($row[$field]) > $width)
                $row[$field] = substr($row[$field], 0, $width - 1).'…';
            echo str_pad($row[$field], $width + $margin, ' ', STR_PAD_RIGHT).'</span>';
        }
        echo "\n";
    }
    echo '</pre>';
}
else
    echo 'Explain query failed.';
?>

Example output
Code:
Field Type Null Key Default Extra id varchar(8) NO PRI email varchar(96) NO UNI username varchar(32) NO UNI password varchar(32) NO init_vector varchar(64) NO last_login_at datetime NO joined_at datetime NO
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Web stats fcolor Web Design Forum 10 12-Jul-2007 09:48
Need Free Website Templates sam_dezine Web Design Forum 8 06-Sep-2006 04:25
Pixel script Template customization gatufats Web Design Forum 2 29-Jun-2006 05:05
Who Is A Web Designer ? alicehopkins Web Design Forum 1 13-Jun-2006 03:56
HTML Code amgujral Web Design Forum 0 07-Apr-2006 10:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.