[Gloucs] mysql query in a php loop?

Mick Brooks gloucs at mailman.lug.org.uk
Tue Jun 17 21:41:00 2003


On Tue, Jun 17, 2003 at 09:14:00PM +0100, Guy Edwards wrote:
> On Tue, 2003-06-17 at 21:13, Mick Brooks wrote:
> [snip]
> > When you say it stops after the first loop iteration, do you mean that....
> > 
> > > for ($i=0; $i <$num_results; $i++)
> > >     {
> > 
> > ...it never gets here for a second time?
> 
> no because if I do
> 
> for ($i=0; $i <$num_results; $i++)
>     {
>      echo "<h1>$i</h1>";
> 
> it outputs as it processes:

0 ????
> 1
> 2
> 3
> 4
> 5
> 
> and stops where 5 is the first item in the database that matches all the
> conditions and is affected by the MySQL update inside the loop. (1-4
> being ditched as they aren't selected by bnum)

So it doesn't re-enter your loop after having run the MySQL query?

> > It's a long shot, but if you really mean that it never enters the loop a
> > second time, it must be because the condition is no longer true - it
> > can't be anything else. This is good because you only need chase
> > $num_results now....
> > 
> > >      $row = mysql_fetch_array($result);
> > >      $req_num = htmlspecialchars(stripslashes($row["itemid"]));
> 
> hmmm... it seems to loop just fine until it hits a selected item. It
> goes through all the if statements for the item and then does the SQL
> query on it... but after that, quits from the loop. no error message and
> loads the page footer below (e.g. just a require("htmlfooter.php"); )
> without any problem.

What are the values of $i and $num_results just before and after the
query?

-- 
Mick Brooks
michael.brooks@physics.ox.ac.uk