mysql_field_flags() Function for PHP

What is mysql_field_flags() function in PHP?
How does mysql_field_flags() works?

Explanation

Mysql function mysql_field_flags() get the flags(not_null, primary_key, auto_increment, blob, binary..etc) associated with the specified field in a result.
Syntax
string mysql_field_flags ( resource result, int field_offset)

Returns a string of flags associated with the result, or FALSE on failure.
mysql_field_flags() returns any flags that are associated with a particular field in a mysql result handle returned by mysql_db_query() or mysql_query().
The flags are reported as a single word per flag separated by a single space (flag1 flag2 flag3 ...), so that you can split the returned value using explode().
The second argument field_offset specifies the desired column for which flags should be returned. The field offset starts at 0.
The list of flags that can be returned is as follows.
  • Flag Name - Description
  • auto_increment - The column has the AUTO_INCREMENT attribute set.
  • binary - The column has the BINARY attribute set. This is set by default for BLOB-type columns.
  • blob - The column is a BLOB type.
  • enum - The column is an ENUM column. Note that there is no corresponding set flag.
  • multiple_key - The column is part of a multi-key index.
  • not_null - The column has the NOT NULL attribute set.
  • primary_key - The column is the PRIMARY KEY.
  • timestamp - The column is a TIMESTAMP column.
  • unique_key - The column has the UNIQUE attribute set.
  • unsigned - The column has the UNSIGNED attribute set.
  • zerofill - The column has the ZEROFILL attribute set.

Example : Display flags of first fields

<?php
//Attempt to connect to the default database server

$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database

if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "n";
}
$query="select * from my_table";
$result=mysql_query($query,$link);
// display flags of first field

$field_flag=mysql_field_flags($result,0);
// used explode to break the result with single space

$ff=explode(' ',$field_flag);
while (list ($key, $val) = each ($ff)) {
// displaying the flags

echo "$key -> $val <br>";
}
mysql_close($link);
?>

In the above code flags of result returned by mysql_query() function is displayed. As the field offset is '0', above code will display flags of first field in the result handle '$result'.
RESULT:
0 -> not_null
1 -> primary_key
2 -> auto_increment

Example: Display flags of all the fields

<?php
//Attempt to connect to the default database server

$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database

if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "n";
}
$query="select * from my_table";
$result=mysql_query($query,$link);
$i = 0;
//loop till end of fields in result handle

while ($i < mysql_num_fields ($result)) {
//fetch field names in result handle

$row = mysql_fetch_field ($result);
//print field names in result handle

echo "$row->name <br>";
//fetch flags of each fields in result handle

$field_flag=mysql_field_flags($result,$i);
$ff=explode(' ',$field_flag);
while (list ($key, $val) = each ($ff)) {
//print flags of all the fields in result handle

echo "$key -> $val <br>";
}
$i++;
}
mysql_close($link);
?>

In the above code flags of each field in result handle is fetched and displayed along with the field name.
RESULT:
ID
0 -> not_null
1 -> primary_key
2 -> auto_increment
TITLE
0 ->
DESCRIPTION
0 -> blob
DATE
0 -> binary

PHP Topics


Ask Questions

Ask Question