View Full Version : select the column names from a table.
james438
02-23-2008, 10:52 PM
how do you select the columns from a table and display the names of the columns in a given table?
james438
02-24-2008, 03:02 AM
here's the answer I came up with:
$result = mysql_query("show columns from tablename");
$count = 0;
while ($row=mysql_fetch_row($result)){
$name.="$row[0] ";
}
$name=explode(" ",$name);
$count_n=count($name);
$a=0;
while ($a<$count_n-1){
echo "$name[$a]<br>";$a++;
}
Leafy
02-26-2008, 12:36 AM
I have a solution pre-built, hooray for you.
You should be able to get the gist of what to do with this. If you don't, you can just use my database class (link in signature).
function getColumns($table) {
$rr = array();
$r = $this->query("SELECT * FROM information_schema.columns".
" WHERE `TABLE_NAME`='$table'",1);
foreach($r as $key => $info) {
$len = $info["COLUMN_TYPE"]; // int(5) <-- 5 is our length
$len = explode("(",$len); // The easiest way I know to get something
$len = $len[1]; // between two strings is to explode twice and get either side.
$len = explode(")",$len);
$len = $len[0];
$rr[$info["COLUMN_NAME"]] = array(
"Column Name" => $info["COLUMN_NAME"],
"Data Type" => $info["DATA_TYPE"],
"Length" => $len,
"Null" => ($info["IS_NULLABLE"] == "YES")
);
}
return $rr;
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.