Answer the question
In order to leave comments, you need to log in
Is it possible to query the database in a loop?
I have an array of elements and I need to find out something about each of them, I do it in a loop and in the body of the loop I make queries to the database (about 100-200 iterations). Is this bad or can it be done?
Answer the question
In order to leave comments, you need to log in
You can, but it's bad for performance.
Consider using IN operator in sql query
This is fundamentally wrong. It is possible to form a line with a query in a loop, and then execute 1 (ONE!) query. Or to form XML and to transfer it in xn \function, besides that there was 1 request. You can't execute queries in a loop (well, or you can if your goal is to shit-code).
The plural in the word "requests" scares me a little. More than one per loop iteration? Such behavior should be avoided as much as possible. If you have a primitive script of the simplest select from one table, then it's not fatal. But at the very least complex scripts are better not to be called by a cycle. Alternatively, glue all the parameters into one string variable through a separator, and on the database server, cut the string into separate components and execute one tricky script.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question