madcrazy1 Posted April 1, 2011 Share Posted April 1, 2011 what is the correct sql query to: drop all tables where row count=0 Thank you Link to comment https://forums.phpfreaks.com/topic/232362-drop-all-table-where/ Share on other sites More sharing options...
madcrazy1 Posted April 1, 2011 Author Share Posted April 1, 2011 Sorry: PHP Version 5.2.2 Link to comment https://forums.phpfreaks.com/topic/232362-drop-all-table-where/#findComment-1195339 Share on other sites More sharing options...
blacknight Posted April 1, 2011 Share Posted April 1, 2011 $username="root"; $password="mypassword"; $database="mydatabase"; mysql_connect('localhost',$username,$password); mysql_select_db($database) or die( "Unable to select database"); function drop_empty_tables(){ $tables = mysql_query('SHOW TABLES'); while($table = mysql_fetch_array($tables)){ $table = $table[0]; $records = mysql_query("SELECT * FROM $table"); if(mysql_num_rows($records) == 0){ mysql_query("DROP TABLE $table"); echo "DROP TABLE $table;\n"; } } } drop_empty_tables(); any table on the server the user has rights to with 0 row count will be droped Link to comment https://forums.phpfreaks.com/topic/232362-drop-all-table-where/#findComment-1195340 Share on other sites More sharing options...
madcrazy1 Posted April 1, 2011 Author Share Posted April 1, 2011 Thank you. It worked nicely! Link to comment https://forums.phpfreaks.com/topic/232362-drop-all-table-where/#findComment-1195348 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.