PHP mysql_fetch_field() Function Example

PHP mysql_fetch_field() Function Example


Posted in : PHP Posted on : February 7, 2011 at 5:04 PM Comments : [ 0 ]

If you want to find out field information then used mysql_fetch_field() function that return an object containing information of a field from the record set.

PHP mysql_fetch_field() Function Example:

If you want to find out field information then used mysql_fetch_field() function that return an object containing information of a field from the record set. The mysql_fetch_field() function gets data from the mysql_query() function and return an object on success or false on failure.

Example:

<?php
$connection = mysql_connect("localhost","root","root");

if (!$connection) {
die('PHP Mysql database connection could not connect : ' . mysql_error());
}
else{ 
mysql_select_db("php_mysql", $connection);
$sql = "SELECT * from user";
$result = mysql_query($sql,$connection);

while ($user_table_property = mysql_fetch_field($result)) {
echo "Field name: " . $user_table_property->name . "<br />";
echo "Table name: " . $user_table_property->table . "<br />";
echo "Default value: " . $user_table_property->def . "<br />";
echo "Max length: " . $user_table_property->max_length . "<br />";
echo "Not NULL: " . $user_table_property->not_null . "<br />";
echo "Primary Key: " . $user_table_property->primary_key . "<br />";
echo "Unique Key: " . $user_table_property->unique_key . "<br />";
echo "Field Type: " . $user_table_property->type . "<br /><br />";
}
}
mysql_close($connection);
?> 

After running this example you can see the output that contain fields information.

Output:

Field name: user_id
Table name: user
Default value: 
Max length: 1
Not NULL: 1
Primary Key: 1
Unique Key: 0
Field Type: int

Field name: user_name
Table name: user
Default value: 
Max length: 6
Not NULL: 0
Primary Key: 0
Unique Key: 0
Field Type: string

Field name: user_email
Table name: user
Default value: 
Max length: 21
Not NULL: 0
Primary Key: 0
Unique Key: 0
Field Type: string

Field name: user_address
Table name: user
Default value: 
Max length: 6
Not NULL: 0
Primary Key: 0
Unique Key: 0
Field Type: string

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics