如何批量删除sql数据库中具有相同前缀的表如:cdb_


想用php做一个卸载程序,在卸载时同时把几个有相同前缀名的表删除,请问这个改怎样写?

sql php mysql

缱绻未眠、 13 years, 8 months ago

可以指定table_pre表前缀:

   
  <?php
  
$table_pre = 'cdb_';
mysql_connect('localhost','user','pwd');
mysql_select_db('databasename');
$rs=mysql_query("show tables like '".$table_pre."%'");

while($data=mysql_fetch_array($rs)){
$tmp_flag=strpos($data[0],$table_pre);
if($tmp_flag===0){
mysql_query("drop table $data[0]");
}
}
?>

或者:

   
  <?php
  
mysql_connect('localhost','user','pwd');
mysql_select_db('databasename');
$rs=mysql_query('show tables');

while($data=mysql_fetch_array($rs)){

$tmp_flag=strpos($data[0],'cdb_');
if($tmp_flag===0){
mysql_query("drop table $data[0]");
}
}
?>

管理员的马甲 answered 13 years, 8 months ago

Your Answer