lordvader Posted May 15, 2008 Share Posted May 15, 2008 I want to add a column to an existing table, but first I have to check if the column exists. I can think of several ways to check: CASE WHEN EXISTS SHOW COLUMNS mysql_list_fields mysql_num_fields mysql_field_name But I was wondering in your experience, what is the simplest most efficient way to check if a column exists, if not, then add it to the table? thanks Link to comment https://forums.phpfreaks.com/topic/105821-best-method-to-check-if-a-column-exists-and-if-not-add-it/ Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 dynamic column adding isn't the strongest idea in databasing whats your motive behind it? Link to comment https://forums.phpfreaks.com/topic/105821-best-method-to-check-if-a-column-exists-and-if-not-add-it/#findComment-542614 Share on other sites More sharing options...
fenway Posted May 16, 2008 Share Posted May 16, 2008 You can use DESCRIBE TABLE, SHOW COLUMNS, or the I_S tables to query the schema, but there's no ADD IF EXISTS.. .you'll need to do it in 2 steps. But you'd better not be making dynamic column names... Link to comment https://forums.phpfreaks.com/topic/105821-best-method-to-check-if-a-column-exists-and-if-not-add-it/#findComment-543023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.