in reply to Extracting values from one table and feed the values as a query for second table
Naturally the question arises in my mind, “is it possible to use a JOIN here?” In my experience, repetitive queries are costly even when you have prepared the query once and keep re-using the same statement handle. I really like to push the chore of finding matching records onto the database engine when I can. I have not examined your code closely.