dynamically alter table is column doesn't exist

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
knipper
Forum Newbie
Posts: 1
Joined: Mon Aug 29, 2005 1:54 pm

dynamically alter table is column doesn't exist

Post by knipper »

Does anyone know how to do this. My situation is that I have an application that has many tables/columns. I want to have a function that examines the query and checks to make sure the columns exist and creates them if not. This would be a housekeeping function for when adding content.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you're goign to have to do it with multiple queries.

you could use MySQL's built in table structure query "desc %tablename%" or you could simply select data from the table then use php to find the field names with mysql_fetch_field().

you'll then need to loop over the results and match them against the field names you know you have. If there is a new one, you'll have to alter the table and add it to the list of fields.
Post Reply