Reputation: 14529
I've tried this: http://be.php.net/manual/en/function.mysql-list-fields.php, but there's too much detail here.
What I'm looking for is a function that returns an array of strings. This array should only contain the names of all columns that a certain table has.
So for instance, if I have a table with these columns:
username | email | gender |age | married | number_of_children | street | province
I should get the same thing as if I did this:
$vars = array('username','email','gender','age','married','number_of_children','street','province');
Is there a PHP function already that can do this? Or shall I need some SQL statements of my own?
Upvotes: 1
Views: 244
Reputation: 340171
I think there is no direct PHP function that will do what you want.
You can:
use a SQL query over the INFORMATION_SCHEMA
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'database' AND TABLE_NAME = 'table'
use the MySQL specific 'desc table' or 'show columns from table' SQL sentences
Upvotes: 1
Reputation: 10721
you could also do
$query = mysql_query("SELECT * FROM `table`");
$result = mysql_fetch_assoc($query);
$keys = array_keys($result);
$keys
will now contain all of the column names because they were the array keys in the $result
array.
Upvotes: 4
Reputation: 32374
Theres a MySQL Query, which returns the column names asa result set.
SHOW COLUMNS FROM tablename
Upvotes: 1