Forums / Craftwork / Server-side / Back to PHP MySQL: A question!

Probably a general question ... but still

fade2k
#1 2012-02-24 17:48

I've gone back to trying to re-learn and learn php/sql .... I have a question about how the mySQL db is being called in a situation like say:

sql query;
assign array var {
assigning results to var from sql array
      sql query;
      assign array var {
      assigning results to var from sql array
      }
}

My question is, is the second sql query being looped.. like for each result... is it a new query for every row from the query? Or is it being queried once after the first query and only grabbing relevent rows?

I know this question is really basic/general but I assumed it was like a foreach loop (on the second query) and if so, it can't be good right?

Thanks!

- Be Kind, Please Rewind -
This post was edited by fade2k (2012-02-24 18:05, 12 years ago)
GHengeveld
#2 2012-02-24 18:07

If you put an SQL query (like $db->query() in Siena or sed_sql_query() in Genoa) within a foreach/for/while loop, the query will be executed for every iteration of the loop. This is considered bad practice because its very inefficient. However in some cases it's the only option you have. If you're going to do a SELECT query it's better to include it in the first query, outside of the loop if possible.

fade2k
#3 2012-02-24 18:15

Thanks! Thats exactly what I wanted to know and is what I expected.

ATM I'm just doing random tests with php/sql to see how things are working -- Haven't really tried ANY (outside of very very slight mods) of it in Siena *yet*.

- Be Kind, Please Rewind -
This post was edited by fade2k (2012-02-25 06:05, 12 years ago)