PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mysql_fetch_field

Name

mysql_fetch_field — Gets the column information for a field in a result handle.

Synopsis

object mysql_fetch_field(result handle[, field_offset]);
mysql result result handle: Result handle returned by mysql_list_fields() , mysql_db_query() , or mysql_query()
int field_offset (optional): Field offset to use

Returns

Object containing column meta-information; FALSE on error

Description

mysql_fetch_field() retrieves the column information for a field in a query. If you specify an offset to mysql_fetch_field() , the column properties for that field are returned. If you don't specify an offset, the data for the next field in order will be returned.

The column information is returned as an object containing one or more of the properties from the following table. (Note: BLOB is an abbreviation of Binary Long OBject).

Property Name Description
blob

Contains 1 if the column contains BLOB values,0 otherwise.

def

The default value of the column.

Note

This property will contain an empty string unless mysql_fetch_field() is fetching column information from a result handle returned by the mysql_list_fields() function.

max_length

The length of the largest value in the result set for the field.

For example, in a query result with three rows, if theuser field contained the values han solo,chewbacca, and R2D2, the max_length property would be 9.



Note

The max_length of a field is not the same thing as the size of the column from which you're retrieving data. A CHAR field with a size of 20 characters may only have a max_length of 9 for a specific result set.



multiple_key

Contains 1 if the column is used in a non-unique index, 0otherwise.

name

Name of the column. If the column's value is generated by an expression, the expression is used as the name. For example, suppose this is the query:

SELECT count(user);

The name property would be count(user).



If an alias is used for the column (or an expression), the alias is used as the name.

not_null

Contains 1 if the column cannot contain a NULL value,0otherwise.

numeric

Contains 1 if the column can only contain numeric data, 0otherwise.

primary_key

Contains 1 if the column is the primary key,0 otherwise.

table

Name of the table containing the column. If the column value is the result of an expression, the table name is an empty string ("").

type

Type of the column. If the column value is generated by an expression, the type of the result determines the type of the column.

For example, suppose this is the query:

SELECT count(user);

The type would be int.

The following values are returned:

Type Description
blob Column containing or expression that returns a Binary Long OBject. This includes all BLOB or TEXT type columns.
date DATE column. Expressions that return a date value are of type int, real, orstring, depending on the value returned.
datetime DATETIME column. Expressions that return a datetime value are of type int, real, orstring, depending on the value returned.
int Column containing or expression that returns integer data. This includes all INT type columns.
null Expression that returns NULL.
real Column containing or expression that returns a floating-point number. This includes the DECIMAL, FLOAT, and DOUBLE column types.
string

CHAR, ENUM, SET, or VARCHAR column, or an expression that returns character data.

Note

Even if the number of characters returned by an expression exceeds the maximum length of 255 characters for a CHAR/VARCHAR column, the type returned is string and not blob, as you might expect.

time TIME column. Expressions that return a time value are of type real or string, depending on the value returned.
timestamp TIMESTAMP column. Expressions that return a timestamp value are of type int.
year YEAR column. Expressions that return a year value are of type int.
unknown Type that doesn't match any type known by mysql_fetch_field() . An occurrence of this type may indicate that the version of MySQL is more recent than the version of PHP.


unique_key Contains1 if the column is part of a UNIQUE key, 0 otherwise.
unsigned Contains 1 if the column can only contain unsigned integers, 0 otherwise.
zerofill Contains 1 if the column contains zero-filled numbers, 0 otherwise.


Version

PHP 3+, PHP 4+

See also

To get information about a server's databases:

mysql_list_dbs()

To get information about a database's tables:

mysql_list_tables()

To get information about a table:

mysql_list_fields()

To find the table name for a field in a result set:

mysql_tablename()

To get information about the fields in a result set:

mysql_list_fields()

mysql_field_flags()

mysql_field_name()



Example

Example 804. Fetch all column data from a result handle returned by mysql_query()

<pre>
<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// Make a simple SELECT query
$query = "SELECT * FROM user";
$mysql_result = @ mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

// Fetch the column data
while ($column_data = mysql_fetch_field ($mysql_result)) {

    // Display the column name
    echo '<font size="+1"><b>', $column_data->name, '</b></font><blockquote>';

    // Show every property of the column that is set
    foreach (get_object_vars ($column_data) as $key => $value) {
        if ($value && $key != 'name') {
            printf ("<b>%'.-24s</b>%s\n", $key, $value);
      }
   }
    echo "</blockquote><br />";
}
?>
</pre>

Example 805. Demonstrate how mysql_fetch_field() handles expressions and aliases

<pre>
<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// Make a simple SELECT query.
// Note how the second call to curtime() is coerced into being a number
// by adding zero (0) to it. Look at the script output for more info.
$query = "SELECT year(curdate()), curtime(), curtime()+0 as time, user() as user";
$mysql_result = @ mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

// Fetch the column data
while ($column_data = mysql_fetch_field ($mysql_result)) {

    // Display the column name
    echo '<font size="+1"><b>', $column_data->name, '</b></font><blockquote>';

    // Show every property of the column that is set
    foreach (get_object_vars ($column_data) as $key => $value) {
        if ($value && $key != 'name')
            printf ("<b>%'.-24s</b>%s\n", $key, $value);
    }
    echo "</blockquote><br />";
}
?>
</pre>

Example 806. Use mysql_fetch_field() and mysql_list_fields() together

<pre>
<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

$table = 'user';
$mysql_result = @ mysql_list_fields ($db, $table)
    or die ("Could not list the fields for table '$table' in database '$db'.");

// Fetch the column data
while ($column_data = mysql_fetch_field ($mysql_result)) {

    // Display the column name
    echo '<font size="+1"><b>', $column_data->name, '</b></font><blockquote>';

    // Show every property of the column that is set
    foreach (get_object_vars ($column_data) as $key => $value) {
        if ($value && $key != 'name')
            printf ("<b>%'.-24s</b>%s\n", $key, $value);
    }
    echo "</blockquote><br />";
}
?>
</pre>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.