user1078494
user1078494

Reputation: 509

drop single mysql table from command line

I'm trying to run the following line from my php script:

for($i=0;$i<count($table_list);$i++){
        $command1='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' DROP TABLE `'.$table_list[$i].'`';
        exec($command1);
}

An array with table names is already created earlier.

Nothing happens, table doesn't get dropped. Of course, exec() is enabled.

When I try to run the following command from console, I just get instructions on how to use the mysql command (just as if I used mysql -?):

mysql -hlocalhost -uUSERNAME -pPASSWORD DATABASENAME DROP TABLE products

I also tried putting table name in quotes, but same thing - no luck.

Can anyone please tell me what I'm missing here?

Upvotes: 1

Views: 329

Answers (2)

ThatOneDude
ThatOneDude

Reputation: 1526

From the documentation, you need to use -e / --execute:

--execute=statement, -e statement Execute the statement and quit. The default output format is like that produced with --batch. See Section 4.2.3.1, "Using Options on the Command Line", for some examples. With this option, mysql does not use the history file.

Try the following from the console: mysql -h localhost -u USERNAME -p PASSWORD -e "DROP TABLE products" DATABASENAME

Or the fixed up script:

for($i=0;$i<count($table_list);$i++){
        $command1='mysql -h '.$mysqlHostName .' -u ' .$mysqlUserName .' -p ' .$mysqlPassword .' -e "DROP TABLE `'.$table_list[$i].'`" '.$mysqlDatabaseName ;
        exec($command1);
}

Upvotes: 0

Meenesh Jain
Meenesh Jain

Reputation: 2528

i think your problem is because you havent given proper space

You have this

mysql -hlocalhost -uUSERNAME -pPASSWORD DATABASENAME DROP TABLE products

and it should be like

mysql -h localhost -u USERNAME -p PASSWORD DATABASENAME DROP TABLE products

see -h & -u & -p have a proper space before and after

Upvotes: 1

Related Questions