/    Sign up×
Community /Pin to ProfileBookmark

Drop table error ??

Hi,

I seam to be gettin this error when i run this code

error = “Incorrect table name ””

[code=php]
$num = 0;
$show = “SHOW TABLES”;
$show_res = mysql_query($show, $conn) or die(mysql_error());
while($row = mysql_fetch_assoc($show_res)) {
$sql = “DROP TABLE `”.$row[$num].”`”;
mysql_query($sql) or die(mysql_error());
$num++;
}
[/code]

Am i doing something wrong or is there a better way to drop all tables in a database

P.S i cant name them individualy as they may change!

Thanks
Adam

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@k0r54authorNov 21.2004 — any ideas?
Copy linkTweet thisAlerts:
@Paul_JrNov 22.2004 — Try something like this:
[code=php]
<?php
$query = mysql_list_tables('database_name');
echo '<ul>';
while($row = mysql_fetch_row($query)) {
echo '<li>', $row[0], '</li>';
}
echo '</ul>';
?>
[/code]

That will print out all the tables in [i]database_name[/i] in an unordered list. See: [url=http://www.php.net/mysql-list-tables]mysql_list_tables()[/url] (Note: This function is deprecated in PHP5.)

[b]***EDIT***[/b]

Sorry, I must've misread the question. :o
Copy linkTweet thisAlerts:
@ShrineDesignsNov 22.2004 — try this:[code=php]<?php
$result = mysql_query("SHOW TABLES FROM database_name", $conn);

while($row = mysql_fetch_array($result, MYSQL_NUM))
{
mysql_query("DROP TABLE {$row[0]}") or die(mysql_error());
}
?>[/code]
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...