Re: DROP tables with prefix match

From: Date: Thu, 16 May 2002 19:33:31 +0000
Subject: Re: DROP tables with prefix match
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-19225@lists.php.net to get a copy of this message
"John Hughes" <jlhughes@yahoo.com> wrote in message news:20020515234043.23015.qmail@web10103.mail.yahoo.com... > I need to rebuild a PHP-mySQL site that has an extensive table > structure. Is there a way to DROP all tables that start with a > certain prefix? First, use SHOW TABLES FROM db LIKE prefix% to get the table names, then iterate through with DROP TABLE tablename The result will look something like <?php $con = mysql_pconnect("localhost", "user", "pwd") or die("Error connecting: ".mysql_error()); $res = mysql_select_db("dbname", $con) or die("Error selecting database: ".mysql_error()); $query = "SHOW TABLES FROM $dbname LIKE '$prefix%'"; $res = mysql_query($query, $con); while ($row = mysql_fetch_array($res)) { $query = "DROP TABLE {$res[0]}" $ex = mysql_query($query) or echo("Error dropping table {$res[0]}:".mysql_error()); } ?> If you are going to use this repeatedly, I would make this a two-stage endeavor: the first step queries the database and returns a check-box form with all the returned table-names; when submitted, the checked tables are dropped. Hope this helps

« previous php.db (#19225) next »