To show field names from a table without fetching the records using PHP and MySQL

This code provide the facility to show field names from a table (and other field meta data information), without fetching the records.

$db=mysqli_connect("localhost","root","abc123","mycompanyhr");
//Check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>

$qfields="SHOW COLUMNS FROM employee";
/*the sql query above will return only the fieldnames,
without any record*/
$rsfield=mysqli_query($db,$qfields);
while($field=mysqli_fetch_array($rsfield)){
echo $field['Field']."<br>";
echo $field['Type']."<br>";
echo $field['Key']."<br>";
echo $field['Null']."<br>";
echo $field['Default']."<br>";
echo $field['Extra']."<br>";
}


Refer here for more on SHOW clause
http://dev.mysql.com/doc/refman/5.1/en/charset-show.html

Popular Posts