SHOW COLUMNS from <table_name>
We will use PHP script to display all details of the columns.
<?Php
require "config.php"; // Database Connection
$result = $dbo->query("SHOW COLUMNS from student");
echo "<table><tr><th>Field</th><th>Type</th><th>Null</th><th>key</th><th>Defaul</th><th>Extra</th></tr>";
while ($row = $result->fetch(PDO::FETCH_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td></tr>";
}
echo "</table>";
?>
<?
$query="select * from student";
$result=mysql_query($query) or die( "query failed");
$field_flag=mysql_field_flags($result,0);
// We are trying for flags of first field
$ff=explode(' ',$field_flag);
// used explode to break the result
// with single space
while (list ($key, $val) = each ($ff)) {
echo "$key -> $val <br>"; // displaying the flags
}
?>
The above code will display the flags associated with the first field of the student table. Now we will try to improve the code and first list all the fields and then use the above code to display all the field flags associated with it.
$query="select * from student";
$result=mysql_query($query) or die( "query failed");
$i = 0;
while ($i < mysql_num_fields ($result)) {
$row = mysql_fetch_field ($result);
echo "<b>$row->name</b> <br>";
$field_flag=mysql_field_flags($result,$i);
$ff=explode(' ',$field_flag);
while (list ($key, $val) = each ($ff)) {
echo "$key -> $val <br>";
}
$i++;
}
The result of the above query is here CREATE TABLE student (
id int(2) NOT NULL auto_increment,
name varchar(50) NOT NULL default '',
class varchar(10) NOT NULL default '',
mark int(3) NOT NULL default '0',
UNIQUE KEY id (id)
) TYPE=MyISAM;
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.