Home > Article > Backend Development > php mysql data table gets field name, length, information_PHP tutorial
The powerful PHP can use the related functions of mysql interaction to obtain the field information of the data table. For example, the data table can be obtained to obtain the field name, field length, field information, etc.
The powerful PHP tutorial can use the interactive related functions of the MySQL tutorial to obtain the field information of the data table. For example, the data table can be obtained to obtain the field name, field length, field information, etc.
*/
$hostname="localhost"; //Define the mysql server name to connect to
$username="root"; //Define the username used to connect
$password=""; //Define the password used to connect
$link=mysql_connect($hostname,$username,$password); //Open mysql connection
$db_list=mysql_list_dbs($link); //List database tutorial
$rows=mysql_num_rows($db_list); //Get the number of returned results
$i=0;
while($i<$rows) //Traverse the result set through a loop and assign it to the object
{
echo mysql_db_name($db_list,$i)."n"; //Output object content
echo "
n";
$i++;
}
mysql_close($link); //Close the mysql connection
//Return the length of the column
$sql_str="select * from friends where id=1"; //Define sql statement
$result=mysql_query($sql_str); //Execute sql statement
$re_a=mysql_fetch_array($result);
$re_len=mysql_fetch_lengths($result);
for($i=0;$i
echo "The length of the ".$i." column of the returned result is: ".$re_len[$i];
echo "
";
}
mysql_close($link);
//Get field information
$result=mysql_query("select * from friends"); //Execute sql query
/*Get field information*/
$i=0;
while($i
$i++;
echo "Information of column ".$i.":
n";
$meta=mysql_fetch_field($result); //Get field information
if(!$meta) //If the value does not exist
{
echo "no information available
n"; //Output no information available
}
echo "
<br> blob: $meta->blob <br> max_length: $meta->max_length<br> multiple_key: $meta->multiple_key<br> name: $meta->name<br> not_null: $meta->not_null<br> numeric: $meta->numeric<br> primary_key: $meta->primary_key<br> table: $meta->table<br> type: $meta->type<br> unique_key: $meta->unique_key<br> unsigned: $meta->unsigned<br> zerofill: $meta->zerofill<br>"; //End format output
//The mysql_field_flags() function obtains the flags associated with the specified field from the result.
$re_field=mysql_field_flags($result,0);
$flag=explode(" ",$re_field);
print_r($flag);
$re_field=mysql_field_flags($result,1);
$flag=explode(" ",$re_field);
//List name
$result=mysql_query($sql_str); //Execute sql statement
$re_name=mysql_field_name($result,0); //Get the name of the first field
echo "The name of the first field is: ".$re_name;
echo "
";
$re_name=mysql_field_name($result,1); //Get the name of the second field
echo "The name of the second field is: ".$re_name;
echo "
";
$re_name=mysql_field_name($result,2); //Get the name of the third field
echo "The name of the third field is: ".$re_name;
echo "
";
$re_name=mysql_field_name($result,3); //Get the name of the fourth field
echo "The name of the fourth field is: ".$re_name;
echo "
";
$re_name=mysql_field_name($result,4); //Get the name of the fifth field
echo "The name of the fifth field is: ".$re_name;
echo "
";