[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP & MSSQL - SQL Loop within another SQL loop?

Posted on 2011-02-28
8
Medium Priority
?
279 Views
Last Modified: 2013-12-13
Hi all,

I'm trying to run a sql query (as a loop) within another sql loop thats already running, unfortunately, im not getting any results from the inner query - a basic example of what im trying to do is:

while($query1 = mysql_fetch_array($querycode1))
  {
      $mainname=$query1['name']
    echo $mainname;
               while($query2 = mysql_fetch_array($querycode2))
                {
                   $othername=$query2['name']
                   echo $othername;
                 }
  }


In that example it doesnt sound logical having a loop within a loop, but the second query uses variables from the first to make it work if that makes sense.

Does anyone know where im going wrong - is it possible to do what im trying to do?!

Thanks
0
Comment
Question by:Bergstr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 34997352
you presumable don't "run" the query2 inside the loop, which you need to do...
0
 
LVL 6

Expert Comment

by:nagyistvan
ID: 34997377
0
 

Author Comment

by:Bergstr
ID: 34997664
Angellll - I'm not sure what you mean by that - by "running" it im just doing the following:

while($query2 = mysql_fetch_array($querycode2))

 - $querycode2 looks like:

$querycode2 = mssql_query("select * from table");

I'm assuming the mssql_query is what you mean by running it..?

nagyistvan - sorry, dont think thats what im looking for... based on the results from the first query, i then need to query another table using data from the initial query, and will get multiple results from the second query, which again i may need to run a loop to go through. this may end up being 4 or 5 levels deep.

Thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34998157
yes, and you need to put that line inside the first loop.

that said, you might to explain a bit more about what you are trying to do, possibly you can solve this with pure sql?
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34998359
Did you mean mssql_query and not mysql_query?

It looks like you're doing a mysql fetch on an mssql query when you should be using mssql_fetch_array.

0
 

Author Comment

by:Bergstr
ID: 34998692
Sorry - the actual code is mssql in all parts of the code - but still doesnt work

Angellll - I'll try my best to explain what im currently doing..! :

The first table is a list of orders - im querying that and just using the order details to check that we have sufficient material to supply the order.

Query 1 = select * from orders

then i want to check all the level-1 materials for stock - if they are short of stock, display them, if they have stock, just ignore them and check the next part.

If a level-1 part IS short, check all level-2 stock levels and so on, until im at the lowest point of assembly.

I dont think there is an easy way i can check it without doing multiple queries as  i need if statements at each level to decide if i need to go further into the parts?
0
 
LVL 8

Expert Comment

by:ropenner
ID: 35032907
structure should have the two commented out lines in it, if your second query depends on the contents returned by the first one.

while($query1 = mysql_fetch_array($querycode1))  {
    $mainname=$query1['name']
    echo $mainname;
// create $query2 $query2 = "SELECT.... FROM ... WHERE ...= $mainname"";
// execute $query2 mysql_query($query2);
   while($query2 = mysql_fetch_array($querycode2))  {
           $othername=$query2['name']
           echo $othername;
     }
  }
0
 

Author Closing Comment

by:Bergstr
ID: 35339741
I've just come back and read this, and looked at my example code, and i can now see exactly what your saying!

I'm not sure if that was the problem, however i got it working - going into multiple levels within a loop - not sure if this was the problem, but looking at the code i posted it looks like it possibly was :)

Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question