0

I have two mysql_queries who have two while loops in fetch_assoc. One written inside another. I want to take one while loop inside a function and another inside another function. How to do that? Help. Here's an example of what I'm trying to say.

$query1 = mysql_query('...');
while($row1 = mysql_fetch_assoc($query1)){
$id = $row1['id'];

$query2 = mysql_query('...');
while($row2 = mysql_fetch_assoc($query2)){ 
$picture = $row2['picture'];

echo $id.' '.$picture;

} }

I want to take these two inside two different functions..

function query1(){
1st query with 1st while loop
}

function query2(){

2nd query with 2nd while loop
}

So that I can execute them just the way it is supposed to be..

query1();
query2();

echo $id.' '.$picture;

How to achieve this? Help.

5
  • If one is inside the other you can't. You could have query1(); which looks sth like this while (...) { query2(); } however Commented Feb 8, 2014 at 14:32
  • You can call query2() inside query1() Commented Feb 8, 2014 at 14:33
  • like this? query1(query1()); ? Commented Feb 8, 2014 at 14:33
  • Inside the body of the query1(), within the while loop. SO you need to only call query1() from the outside. Commented Feb 8, 2014 at 14:34
  • For performance, avoid queries in loop, prefer to use 2 queries and use IN() to select values from the other table. (You will have 2 loops but with only 2 queries) Commented Feb 8, 2014 at 14:41

1 Answer 1

1

You could structure it as follows:

function query2(){
    //do inner stuff
}
function query1(){
    //do stuff
    query2();
}
query1();

Let me know if you need more context!

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. Your code opened many paths.. I'm trying different things now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.