0

I´m trying to delete multiple records on multiples tables at once

Using an sql script into php mysql_query function :

$sql= " delete from table1 where id = '$id';
        delete from table2 where id = '$id';
        delete from table3 where id = '$id';";
mysql_query($sql, $connect);

But it not seems to be working, is there some mistake ? Is there other way to do that ?

antoniogbn
  • 57
  • 8
  • 6
    Stop using the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Apr 25 '17 at 19:20
  • 1
    is there a reason you can't call it 3 times? – Forbs Apr 25 '17 at 19:22
  • 1
    Possible duplicate of [Mysql - delete from multiple tables with one query](http://stackoverflow.com/questions/4839905/mysql-delete-from-multiple-tables-with-one-query) – Don't Panic Apr 25 '17 at 19:23
  • Three tables? Three queries. Doing this as one big multi-query is a mistake. – tadman Apr 25 '17 at 19:24
  • @alex is right. There is another way also. i.e. to use foriegn key constraints on these tables and with the` on delete cascade ` option – Imdad Apr 25 '17 at 19:24

1 Answers1

0

Try using a transaction , it will provide atomicity, consistency , isolation and durability out of the box and gives better result in terms of performance as well . AS far as mysql API is concerned try moving on to PDO or MYSQLi for that matter .

Maaz Rehman
  • 674
  • 1
  • 7
  • 20