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 filed 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 filed ( index=0 )

Related Tutorial
Printing Error message
Posting error message to an email address
PHP MySQL data display
<?
$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;

Discuss this tutorial at forum

List of SQL Tutorials


Scripts
PHP
JavaScript
SQL Tutorial List
SQL (Home)
mysql_affected_rows mysql_change_user mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_query mysql_result mysql_select_db mysql_tablename
SQL site Map
Knowledge Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.