Napoleon001 Posted May 17, 2006 Share Posted May 17, 2006 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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/ Share on other sites More sharing options...
fenway Posted May 17, 2006 Share Posted May 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/#findComment-36624 Share on other sites More sharing options...
crouchjay Posted May 17, 2006 Share Posted May 17, 2006 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 | +--------------------------------------------------------------------+ Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/#findComment-36625 Share on other sites More sharing options...
Napoleon001 Posted May 17, 2006 Author Share Posted May 17, 2006 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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/#findComment-36635 Share on other sites More sharing options...
ryanlwh Posted May 17, 2006 Share Posted May 17, 2006 use SHOW COLUMNS FROM table LIKE 'pattern'. Then you don't have to loop through the DESC result. Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/#findComment-36716 Share on other sites More sharing options...
bluebyyou Posted June 22, 2007 Share Posted June 22, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/#findComment-280523 Share on other sites More sharing options...
bluebyyou Posted June 22, 2007 Share Posted June 22, 2007 I just tried it, and the answer was yes mysql_num_rows() works the same on that as any other result. Quote Link to comment https://forums.phpfreaks.com/topic/9858-solved-php-to-detect-if-a-mysql-table-column-exists/#findComment-280529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.