how to delete tables with a prefix

This is a discussion on "how to delete tables with a prefix" within the Databases section. This forum, and the thread "how to delete tables with a prefix are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 17th, 2008, 17:08
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
how to delete tables with a prefix

How do I delete tables with a certain prefix eg that starts with php_ all at once. Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 17th, 2008, 17:21
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

Try:
Code: Select all
DROP TABLE php_
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 17th, 2008, 17:29
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

will that delete all the tables including like this php_zebra
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 17th, 2008, 17:31
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

That was wrong actually.
I think this should work but I'm not entirely sure.
Code: Select all
DROP TABLE php_*
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 17th, 2008, 17:32
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

Code: Select all
mysql> drop table phpbb_*
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '*' at
 line 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 17th, 2008, 18:18
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

This php should work, found it on a forum.
PHP: Select all

<?php 
//assuming you're connected and have selected the db already 
$result mysql_query("SHOW tables"); 
for (
$i 0$i mysql_num_rows($result); $i++) 

 
$tablename mysql_result($result$i0); 
 if (
substr($tablename03) == "php"
 { 
 if (
mysql_query("drop TABLE ".$tablename)) print "droped ".tablename."!<br />"
 } 

?>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jan 17th, 2008, 18:49
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

lemmi try it out
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jan 17th, 2008, 19:24
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

nothing happens the page is just blank doesn't echo the result droped so nothing has happend and the tables are still there
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jan 17th, 2008, 19:25
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

You have put in your connection string yeah?
Try replacing
:
PHP: Select all

$result mysql_query("SHOW tables"); 

with:
PHP: Select all

$result mysql_query("SHOW tables") or die(mysql_error()); 

Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jan 17th, 2008, 19:50
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

This is how i connected to my database. Is it right? I still get a blank page
PHP: Select all

$db_host "localhost";
    
$db_user "my_user";
    
$db_password "my_passwd";
    
$db_name "my_name";
    
$connection = @mysql_connect($db_host$db_user$db_password) or die("error connecting");
    
mysql_select_db($db_name$connection); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jan 17th, 2008, 20:01
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

Did you try the code I posted?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jan 17th, 2008, 20:09
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

yes the whole thing looks like this now

PHP: Select all

<?php 
    $db_host 
"localhost";
    
$db_user "my_user";
    
$db_password "my_passwd";
    
$db_name "my_name";
    
$connection = @mysql_connect($db_host$db_user$db_password) or die("error connecting");
    
mysql_select_db($db_name$connection);
    
//assuming you're connected and have selected the db already 
    
$result mysql_query("SHOW tables") or die(mysql_error()); 
    for (
$i 0$i mysql_num_rows($result); $i++) 
    { 
     
$tablename mysql_result($result$i0); 
     if (
substr($tablename03) == "phpbb_"
     { 
     if (
mysql_query("drop TABLE ".$tablename)) print "droped ".tablename."!<br />"
     } 
    } 
    
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Jan 17th, 2008, 20:57
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

You're not getting any error?
Remove the @ before the mysql_connect.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Jan 18th, 2008, 06:09
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

no error at all. i have removed the @ and still thesame. a blank page
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Jan 18th, 2008, 06:17
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: how to delete tables with a prefix

Try changing:
PHP: Select all

  $connection = @mysql_connect($db_host$db_user$db_password) or die("error connecting");
    
mysql_select_db($db_name$connection); 
to:

PHP: Select all

mysql_connect($db_host$db_user$db_password) or die("error connecting");
    
mysql_select_db($db_name); 
Otherwise I dont know.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Jan 19th, 2008, 19:23
Reputable Member
Join Date: Mar 2007
Location: Kenya
Age: 20
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
Re: how to delete tables with a prefix

still no output. just let it go i will work with phpMyAdmin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
delete by checkbox FlashO Classic ASP 3 Mar 30th, 2008 08:42


All times are GMT. The time now is 15:19.