0

I want to delete specific row from multiple mysql tables at a time. The tables have a common column.

I save the table names in an array like this :

$tables = array('table1','table2','table3','table4');
foreach($tables as $table) {
$query = $con->query("DELETE FROM $table WHERE Column='somevalue'");
}

if($query === FALSE) {
echo "Error : " . $query . $con->error;
}

Is this a way to delete rows from multiple tables at once? Please excuse me if this is a silly question. I am not an expert in PHP and MySql..

Hiranmoy Chatterjee
  • 195
  • 1
  • 2
  • 11

1 Answers1

1

To delete tables you have to use the drop command. Just use DROP with the mysql tables that you want to drop separated by commas:

ex: DROP TABLE T1, T2, T3

user2977636
  • 2,086
  • 2
  • 17
  • 21
  • I have to delete specific rows from multiple tables, not entire tables. – Hiranmoy Chatterjee Jun 07 '15 at 07:42
  • Ah okay. Then the way you have should work. However, it would be vulnerable to a MySQL injection attack if any part of the sql string is coming from the user. If this is the case, look into escaping the data you are getting from the user. – user2977636 Jun 07 '15 at 17:20