Forum Moderators: coopster

Message Too Old, No Replies

dynamically adding/removing columns from db table

         

cdmn

4:30 pm on Oct 29, 2007 (gmt 0)

10+ Year Member



Hi.

i need to be able to add or remove db table column.
First solution to come in mind is to use ALTER TABLE query.
But somehow it doesnt seem to be the right choise :-)

The task is simple realy. There is a data table and the user must can to add additional column or remove one. And i, somehow must track what he did, couse ill be running select/update and other queries on that table, so i must know which columns exists etc..

Maybe someone has done something similar and could share his thoughts :-)

d40sithui

4:56 pm on Oct 29, 2007 (gmt 0)

10+ Year Member



i guess one way to track is u can create another table. so when he/she runs a query, u can insert the user, that query itself, and results into this table.
to add/drop a column, its just 'alter table tableName add/drop columnName'

cdmn

1:04 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



The only solution to come in mind is something similar like you wrote :-)

Maybe some more advices? Someone?

jatar_k

1:12 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why would you want users being able to add and remove columns from your db?

this doesn't seem like the best way to go about things.

though if this is what you feel you need to do you need to use ALTER TABLE to do the actual table changes

a good way to find needed functions is to search the main page for those set of functions
[php.net...]

there is a little function here that might help
[webmasterworld.com...]

henry0

1:41 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I feel like jatar_k
You could be better of finding another solution
For example add another col with only “y” or “n”, 1 or 0 etc…

if “y” then you act as if the col you are looking for is there

if “n” then act as if the col “does not exist”

just add in your query WHERE y_n=’$y_n’

<edit>
On the top of that
it is always a good practice to limit as much as possible user's DB privileges.
they should not be allowed to deal with your DB cols
</edit>

cdmn

1:00 pm on Nov 4, 2007 (gmt 0)

10+ Year Member



Thanks for your answers :-)