Tuesday, February 28, 2012

How to get database Table structure detail ?



<?php
$db="dbname";
$username="root";
$password="";
$host="localhost";

$connection = @mysql_connect($host,$username,$password,false,2) or die("Unable to connect Mysql server");
@mysql_select_db($db,$connection) or die ("unable to connect Database");

$result = mysql_query('select * from orders');
if (!$result) {
    die('Query failed: ' . mysql_error());
}
/* get table column data */
$i = 0;
while ($i < mysql_num_fields($result))
{

echo "Information for column $i:<br />\n";
$data = mysql_fetch_field($result, $i);
if (!$data) {
echo "No information available<br />\n";
}
echo "<pre>
blob:         $data->blob
max_length:   $data->max_length
multiple_key: $data->multiple_key
name:         $data->name
not_null:     $data->not_null
numeric:      $data->numeric
primary_key:  $data->primary_key
table:        $data->table
type:         $data->type
unique_key:   $data->unique_key
unsigned:     $data->unsigned
zerofill:     $data->zerofill
</pre>";
$i++;
   }
mysql_free_result($result);
?>


Result
-----------


Information for column 0:

blob:         0
max_length:   2
multiple_key: 0
name:         orders_id
not_null:     1
numeric:      1
primary_key:  1
table:        orders
type:         int
unique_key:   0
unsigned:     0
zerofill:     0

.
.
.
.
.
Information for column 10:

blob:         0
max_length:   2
multiple_key: 0
name:         order_name
not_null:     1
numeric:      1
primary_key:  0
table:        orders
type:         string
unique_key:   0
unsigned:     0
zerofill:     0



No comments:

Post a Comment