SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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

mysql_field_flags 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");
$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
id
0 -> not_null
1 -> primary_key
2 -> auto_increment
name
0 -> not_null
class
0 -> not_null
mark
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) ) TYPE=MyISAM;




Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
SQL Tutorial List
SQL site Map