Friday, March 22, 2013

Check stucture of any DB table in PHP

We can use two SQL commands to get the structure of any table as shown in the code below :

Command format :
1. EXPLAIN  table_name;
2. DESCRIBE table_name;

PHP Code :


<?php
// MySQL Connect
$con = mysql_connect("localhost","root","");


// Select DB
mysql_select_db("test_db", $con);


// RUN QUERY
$res = mysql_query("EXPLAIN employee");


// Fetch Data
while( $row = mysql_fetch_assoc($res))
{
  print_r($row);
}
?>


Output :
Array
(
    [Field] => fname
    [Type] => varchar(100)
    [Null] => NO
    [Key] =>
    [Default] =>
    [Extra] =>
)
Array
(
    [Field] => mname
    [Type] => varchar(100)
    [Null] => NO
    [Key] =>
    [Default] =>
    [Extra] =>
)

...

The above output clearly displays details of a table 'employee' within the DB 'test_db'. The  columns are 'fname', 'lname' and do on ... 

No comments: