Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 Napoleon001

Napoleon001
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 02:39 PM

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

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 May 2006 - 03:07 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 crouchjay

crouchjay
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 17 May 2006 - 03:07 PM

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 |
+--------------------------------------------------------------------+

#4 Napoleon001

Napoleon001
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 03:28 PM

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




#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 May 2006 - 08:03 PM

use SHOW COLUMNS FROM table LIKE 'pattern'. Then you don't have to loop through the DESC result.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 bluebyyou

bluebyyou
  • Members
  • PipPipPip
  • Advanced Member
  • 234 posts
  • LocationIllinois

Posted 22 June 2007 - 10:30 PM

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?
As we enjoy great advantages from the inventions of others, we should be glad of an opportunity to serve others by an invention of ours." - Ben Franklin

#7 bluebyyou

bluebyyou
  • Members
  • PipPipPip
  • Advanced Member
  • 234 posts
  • LocationIllinois

Posted 22 June 2007 - 10:46 PM

I just tried it, and the answer was yes mysql_num_rows() works the same on that as any other result.
As we enjoy great advantages from the inventions of others, we should be glad of an opportunity to serve others by an invention of ours." - Ben Franklin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users