dennismonsewicz Posted December 17, 2008 Share Posted December 17, 2008 I have a SQL command that drops 3 different tables if they exist. can you make this into one sql statement? Example: DROP TABLE IF EXIST `table1`, `table2`, `table3`; Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/ Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Why not find out for yourself? http://dev.mysql.com/doc/refman/5.1/en/drop-table.html According to that, yes you can. Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718189 Share on other sites More sharing options...
dennismonsewicz Posted December 17, 2008 Author Share Posted December 17, 2008 i actually found that site right after i posted the question... sorry for not googling the answer myself! Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718195 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Can you drop a table that doesn't exist... Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718199 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Can you drop a table that doesn't exist... The IF EXISTS part is to throw an error. This is used mainly for development/installer scripts. Incase someone is re-installing or you dont want to alter your tables you can just drop the table if it exists then re-create it. I use it all the time in my installer scripts. Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718203 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2008 Share Posted December 17, 2008 Edit: Basically says the same as above ^^^ That syntax is used to prevent an error if the table does not exist when you try to drop it. Otherwise, you must first query to determine if it exists before you execute the DROP statement. Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718206 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 I know this topic is solved but I found this little function online that is a little more versatile. function table_exists($table) { $connection = mysql_connect($myinfo_host, $myinfo_user, $myinfo_pass) or die(mysql_error()); mysql_select_db($myinfo_base, $connection) or die(mysql_error()); if(!(mysql_query("SELECT * FROM $table"))) { //what to do if table doesn't exist } mysql_close($connection); } Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718209 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 I know this topic is solved but I found this little function online that is a little more versatile. function table_exists($table) { $connection = mysql_connect($myinfo_host, $myinfo_user, $myinfo_pass) or die(mysql_error()); mysql_select_db($myinfo_base, $connection) or die(mysql_error()); if(!(mysql_query("SELECT * FROM $table"))) { //what to do if table doesn't exist } mysql_close($connection); } But why when DROP TABLE IF EXISTS is soo much simpler than doing that? lol Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718213 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 My whole point was to put it in a function so that you can have more conditionals. It's also better if you're going to invoke this function a lot. Quote Link to comment https://forums.phpfreaks.com/topic/137440-solved-drop-table-if-exists-question/#findComment-718222 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.