Jump to content

[SOLVED] PHP to detect if a MySQL table column exists


Napoleon001

Recommended Posts

Hi,

Is there a way for PHP to detect if a MySQL table column exists?

I know how to query a table and a particular column and if it doesn't exist it gives an error so if I had to do it a messy way I'll have to do that and just use the error suppression on that query but is there a proper way to do it?

If you want to read more details about my question:
It is because I have multiple sites linked together and some have multiple languages while others only have one. I keep the contents of each language in a column Contents_EN or Contents_FR.
I want the language selector to simply check what columns are available in this site table and display the options if any but I don't really want to do it the messy was as above and I don't really wan't to have a separate table with this info in it.

Any ideas/suggestions greatly appreciated!

Many thanks! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
You'd have to run a DESCRIBE TABLE and parse the result for your desired column; actually, I think that SHOW COLUMNS has a LIKE option, so maybe you can just check to see if this returns any results. Incidentally, I think that MySQL 5's information schema makes this much easier now.
I am not sure how others due it or if there is a simplar way but you can go

"DESCRIBE table"

And that will return

+--------------------------------------------------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+----+------+---------+------------------+
| id | int(11) | | PRI | | auto_increment |
+--------------------------------------------------------------------+
Thanks for your insight fenway & crouchjay.

I think I need to read up about Describe a bit more. I familiar with it when querying MySQL in a Command Line but not with PHP script... what am I missing out on... [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]


  • 1 year later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.