473,513 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

while function

ddtpmyra
333 Contributor
I have two queries in two diffrent tables my question is 'can i add two queries inside my while loop? How to do that?'
Expand|Select|Wrap|Line Numbers
  1. while ($row = mysql_fetch_array ($result))
Dec 2 '08 #1
2 1224
dlite922
1,584 Recognized Expert Top Contributor
no, a while loop only takes one argument.

What you can do is have two separate while loops, or have a nested while loop.

You can query the DB with an inner while loop, inner query if you need a piece of data from your outer loop, outer query.

However I *strongly* disagree with this. It is very resource intensive and proves the structure of your database is poor (see Database Normalization).






Dan
Dec 2 '08 #2
Atli
5,058 Recognized Expert Expert
I would have to agree with Dan.
This sounds like you need to rethink your queries rather than restructure your code.

Maybe we could be a bit more specific if you showed us the queries you are using.

@dlite922
Technically not true.

A while loop doesn't take any arguments. It does execute a statement on every loop, however.
This statement can do whatever you want it to, for example:
Expand|Select|Wrap|Line Numbers
  1. $result1 = mysql_query("SELECT * FROM someTable");
  2. $result2 = mysql_query("SELECT * FROM someOtherTable");
  3.  
  4. while(
  5.   ($row1 = mysql_fetch_assoc($result1)) or 
  6.   ($row2 = mysql_fetch_assoc($result2))
  7. ) {
  8.   echo "From someTable:\n";
  9.   foreach($row1 as $_key => $_value) {
  10.     echo " - $_key = $_value\n";
  11.   }
  12.   echo "From someOtherTable:\n";
  13.   foreach($row2 as $_key => $_value) {
  14.     echo " - $_key = $_value\n";
  15.   }
  16. }
  17.  
This would run through two result objects, printing ever row of each one.
I wouldn't recommend using this tho, as there are much better alternatives.
Just pointing out the possibility :)

You can even execute simple programs within that statement, without adding anything to the actual code block.
For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. while(
  3.     (($i++ or true) // Always adds 1 to $i and evaluates true
  4.     and print(" Row $i" . PHP_EOL)) // Prints a line.
  5.     and ($i < 10) // Breaks the loop once $i reaches 10
  6. )
  7. {
  8.   // An empty code block! :D
  9. }
  10. ?>
  11.  
This would loop from 1 to 10, printing a line to the output for every loop.
Again, not exactly something you would want to use in a real program, but you get the point.
Dec 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1288
by: Bluexcell | last post by:
While: This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates. The problem with using While as a scanner loop is the loop cycle is carried out at the natural cycle...
2
3879
by: Tobin Fricke | last post by:
I have a wrapper function I use to check the error conditions of various functions: wrap(foo(1,2,3)); (1) while (1 == wrap(bar("fluffy"))) { ... } (2) The wrapper function looks like this: int wrap(int code) { if (code) print_message(code);
3
9458
by: RobG | last post by:
A little while ago I opined that do/while loops are harder to read than for loops, and therefore I preferred using for loops. However, it was pointed out that do/while has significant performance benefits as evidenced by: <URL:http://www.websiteoptimization.com/speed/10/10-2.html> (There's a bug in the page, testLoop is both a function...
52
21854
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
20
1690
by: sathyashrayan | last post by:
What is the use of the above do-while. Is it simply a style issue. Since the above same condition will applied with out a do-while(0); because the loop executes only once. I went through the faq section 10.4 but I am little understood. Any answers please? -- "combination is the heart of chess" A.Alekhine Mail to:
2
2489
by: Adrian | last post by:
hi I make the following call xmlResponse = xws.SubmitXml(Text, xmlRequest.DocumentElement, xmlFilter.DocumentElement) It returns what I want but I would like to do a "do while loop" while I wait for the response 2- 10 secs so I can place some graphic or some thing to show its not locked up...
4
7995
by: Gary | last post by:
Hi, I get this error " " when my web page run, what does it mean? Hope someone can help!!! Gary
6
2411
by: Shawn Minisall | last post by:
I've been having some problems with using a while statement for one menu within another while statement for the main menu, first time I've done it. It's with choice number two from the menu. When I run the program, I get a UnboundLocalError: local variable 'ai' referenced before assignment. I initialize ai as "", but then it just skips to...
1
1347
by: sonyb | last post by:
In this program , I informed the user to enter 3 random numbers .. Each one of these numbers were taken and entered into an function (2x + 5) and after the 3rd number was inserted into this equation , the user was asked to figure out what function was used to ahieve that answer. I have a while loop, that compares the selection to the...
1
3379
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.