Sunday, July 8, 2012

Get mysql table colum information php


Get mysql table colum information php  

You coud see the MySQL colomn information by using following information

Codes
<?php
$conn = mysql_connect('localhost', 'root', '');
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('my_database');
$result = mysql_query('select * from namelist');
if (!$result) {
    die('Query failed: ' . mysql_error());
}
/* get column metadata */
$i = 0;
while ($i < mysql_num_fields($result)) {
    echo "Information for column $i:<br />\n";
    $meta = mysql_fetch_field($result, $i);
    if (!$meta) {
        echo "No information available<br />\n";
    }
    echo "<pre>
blob:         $meta->blob
max_length:   $meta->max_length
multiple_key: $meta->multiple_key
name:         $meta->name
not_null:     $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:         $meta->type
unique_key:   $meta->unique_key
unsigned:     $meta->unsigned
zerofill:     $meta->zerofill
</pre>";
    $i++;
}
mysql_free_result($result);
?>
Results
Information for column 0:
blob:         0
max_length:   2
multiple_key: 0
name:         id
not_null:     1
numeric:      1
primary_key:  1
table:        namelist
type:         int
unique_key:   0
unsigned:     0
zerofill:     0
Information for column 1:
blob:         0
max_length:   10
multiple_key: 0
name:         FirstName
not_null:     0
numeric:      0
primary_key:  0
table:        namelist
type:         string
unique_key:   0
unsigned:     0
zerofill:     0
Information for column 2:
blob:         0
max_length:   9
multiple_key: 0
name:         LastName
not_null:     0
numeric:      0
primary_key:  0
table:        namelist
type:         string
unique_key:   0
unsigned:     0
zerofill:     0

1 comment:

  1. Thanks for sharing this information. Actually PHP scripts is very simple to design, but sometimes I face problems while designing this.

    ReplyDelete