-2

In my database the Owning table has foreign key(Ownerid) which references the Owner id.After some steps I would like to delete all the Owner who's id isn't in the Owning table. I have learnt how to delete with 'Delete' button a specific record but can't figure out how to delete more.This is my start:

$result = mysqli_query($link, "SELECT *
        FROM Owner
        WHERE Owner.id NOT IN (SELECT Ownerid FROM Owning )
        ");
    
while(  $rowo = mysqli_fetch_array($result)):
    /*TODO*/
    
endwhile;

$link is for the database connection

jacky
  • 73
  • 7
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman May 11 '21 at 12:13

1 Answers1

0

You only need one query for this, and no loops. You are so nearly there with your SELECT query.

Use this query to do the whole job:

$result = mysqli_query($link, 'DELETE FROM Owner WHERE id NOT IN (SELECT Ownerid FROM Owning)');