mysql_list_fields() function in PHP

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

Explanation

The mysql_list_fields() function list given MySQL table fields.
Syntax
resource mysql_list_fields ( string database_name, string table_name [, resource link_identifier])

Returns result pointer resource on success, or FALSE on failure.
mysql_list_fields() retrieves information about the given table name, when the database and the table name is passed as arguments. This function returns a result pointer to retrieve field information, which can be used with mysql_field_flags() to list field flags of the specified field, mysql_field_len() to list maximum length of the specified field, mysql_field_name() to list name of the specified field index, and mysql_field_type() to list field type of specified fields.
This function is deprecated. It is preferable to use mysql_query() function to execute SQL query 'SHOW COLUMNS FROM table_name [LIKE 'name'] to retrieve field information of given table .
Example

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//returns result pointer of field informatin
$fields = mysql_list_fields("database1", "table1", $link);
//returns number of fields
$columns = mysql_num_fields($fields);
//list name of fields returned by result pointer
for ($i = 0; $i < $columns; $i++) {
echo mysql_field_name($fields, $i) . "n";
}
?>

Above code returns a result pointer to retrieve field information using mysql_list_fields function for a given connection $link. mysql_num_fields function is used to fetch the number of returned fields and fields names returned by result pointer is listed by using mysql_field_name function.
RESULT:
field1
field2
field3...

PHP Topics


Ask Questions

Ask Question