PHP MySQL Functions to get the flags of a field type from a result set

By using PHP PDO we can display all flags associated with the field or columns of a record. Here is the query
SHOW COLUMNS from <table_name>
We will use PHP script to display all details of the columns.
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>";


This function is used to collect flags associated with a field in a table. It takes a result set and field offset value of the field and returns all the flags. Here all returned flags are separated by a single space so we can use explode function of PHP to create an array of flags. Here we will try to first display the flags of a field of student table and then we will use the same code to display the flags of all the fields. For this, we will first use mysql_fetch_field to list all the fields of a table and then we will try to use the mysql_field_flags function to display all the flags of those fields. Let us start with displaying the flags associated with the first field ( index=0 )

$query="select * from student";
$result=mysql_query($query) or die( "query failed");

// 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>";
$ff=explode(' ',$field_flag);
while (list ($key, $val) = each ($ff)) { 
echo "$key -> $val <br>"; 

The result of the above query is here
0 -> not_null
1 -> primary_key
2 -> auto_increment
0 -> not_null
0 -> not_null
0 -> not_null

The sql dump of student table 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)

PHP MySQL functions mysql_fetch_length() : Length of Data MySQL Error

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    SQL Video Tutorials

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer