Jump to content

Archived

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

Napoleon001

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

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\" /]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 |
+--------------------------------------------------------------------+

Share this post


Link to post
Share on other sites
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\" /]


Share this post


Link to post
Share on other sites
use SHOW COLUMNS FROM table LIKE 'pattern'. Then you don't have to loop through the DESC result.

Share this post


Link to post
Share on other sites
So can I use mysql_num_rows() on the result from "SHOW COLUMNS FROM table LIKE 'pattern'" Or is there some other function to achieve the same thing?

Share this post


Link to post
Share on other sites
I just tried it, and the answer was yes mysql_num_rows() works the same on that as any other result.

Share this post


Link to post
Share on other sites

×

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.