PHP/MySQL: Get Field Names from Table?

  • Thread starter Thread starter adz
  • Start date Start date
A

adz

Guest
Hi,

How can I retrieve the field names from a table in a MySQL database using PHP? I''ve used the SHOW COLUMNS from the table, but it returns all the field names plus their attributes (such as datatype) - I need to single out the field name alone.
I've also tried retrieving data from the information_schema table but that returns nothing, and I've tried the depreciated function mysql_list_field but that returns the same as SHOW COLUMNS.

Any help would be appreciated.

I'll post the code I have so far in additional info.
<?php
include('includes/dbconnect.php');

$category= mysql_real_escape_string($_GET["cat"]);

if (isset($category)){
$sql="SHOW COLUMNS FROM ". $category;
}else{
die('No Category Defined');
}

$result = mysql_query($sql) or die(mysql_error());
echo $sql;
echo "<form>";
while ($get_info = mysql_fetch_row($result)) {
foreach ($get_info as $column) {
echo '<select name="'.$column .'">';
echo '<option value="'. $column . '">'. $column .'</option>"';
echo '</select>';
echo '<br />';
}
}
echo "</form>";

include('includes/dbkill.php');

?>
 
You will need to query the information_schema in the table columns were you will give as parameter the db name and table name... then you will be able to read only the column name for that matter. You must have access to the information schema to grab this information. Good luck.
 
well, you could use

mysql_fetch_assoc($result)

instead of

mysql_fetch_row($result)

and then use the key() php function.
 
Back
Top