Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure why you are trying to do this at the app layer. MySQL provides the ability to delete child rows using foreign key constraints (ON DELETE CASCADE). Check your current schema and see if they are already available: </p> <pre><code>mysql&gt; show create table child_table; </code></pre> <p>If there is a foreign key constraint available you should see something like:</p> <pre><code>CONSTRAINT `constraint_id` FOREIGN KEY (`some_id`) REFERENCES `some_table` (`id`) ON DELETE CASCADE </code></pre> <p>What this means is if you delete a row from 'some_table', any rows in 'child_table' which reference the same id as 'some_table' will automatically be deleted.</p> <p><a href="http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html</a> for more info.</p> <p>If you need to do this at the app layer then you should really consider a transaction, I am assuming you are using PDO, although it may work with mysqli. Below is a potential solution, as such I assume no responsibility for any data loss you may incur. </p> <pre><code>$this-&gt;db-&gt;beginTransaction(); try { $this-&gt;db-&gt;execute('DELETE FROM table_1 WHERE id = 65'); $this-&gt;db-&gt;execute('DELETE FROM table_2 WHERE rela = 65'); $this-&gt;db-&gt;execute('DELETE FROM table_3 WHERE h_id = 11'); $this-&gt;db-&gt;execute('DELETE FROM table_4 WHERE rela = 65'); $this-&gt;db-&gt;execute('DELETE FROM table_5 WHERE h_id = 11'); $this-&gt;db-&gt;commit(); } catch (Exception $e) { error_log($e-&gt;getMessage()); $this-&gt;db-&gt;rollBack(); $e = null; // PHP is notorious for not cleaning exceptions very well so force it } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload