mysql_field_name Functions to get name of the fields of a tableBy using the simple query 'SHOW COLUMNS FROM table' we can get the column names. This query will list out the column names. To display all the records of a table we will use this to show column names. Here is the sample code.
Above code will display the columns but we don't know how many columns are present. (We can keep a counter inside PHP while loop but let us know by using query.) Here is the query with code to display number of columns.
Above code we used and prepared a full code to display all the records present in a table. We have only provided table name as input to the script, column names with records are displayed on its own.
Here is the complete code.
This old MySQL extension was deprecated in PHP 5.5.0 and was removed in PHP 7
Use mysqli_fetch_field_direct() in place of mysql_field_name(). mysql_field_nameThe function mysql_field_name returns the name of the field we supply as field index. For example let us use one simple table student where we have four columns ID, name, class & mark. These four columns name we have specified while creating or modifying the table. We can get these table names by using mysql_field_name function.Here is the syntax
As you have seen this function takes two inputs, one is the result of a mysql_query() function, the other one is the index of the field. The index of the first field is 0 ( zero ) and for second field it is 1. So if we want the header ( column name ) of fourth field then we have to use 3 as index in the mysql_field_name.
If we don't know the index of the field or don't know how many fields are there in a table then we can use mysql_num_fields function. Once we know the total number of fields in a table then we can use them as field index to get the field name. Here is the code for displaying the field names of any table. ( It is assumed that you have active connection to mysql and database selection is done )
The output of the above code is here for our student table. id name class mark Now let us add data also here and display all the records along with the table headers , here is the full code.
Download sql dump of student tableSubscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
![]() ![]() | ||||||||
| ||||||||