PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_field_flags() Function for PHP


Tutorials »Php »

Topic

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






Other Links

web hosting