Problem with loops

The coding below works perfectly when the DO loop only contains an echo statement. As soon as I add the SELECT query to the DO loop it goes wrong. The DO loop performs once correctly and as does the WHILE loop before stopping.

Any ideas?


$query  = "SELECT * FROM books";
$result = mysql_query($query)
      or die('mysql error ' . mysql_error() . ' in query ' . $query);
  WHILE ($row = mysql_fetch_array($result))

  {
    $splitkeywords1 = $row['keywords'];
    $splitkeywords2 = explode(',',$splitkeywords1);
    $keyindex = -1;

  DO
    {
      $keyindex++;
      $newtab = $splitkeywords2[$keyindex];
      echo $newtab;
      $query  = "SELECT * FROM $newtab";
      $result = mysql_query($query)
        or 
       tablecreation($newtab);
    }

  WHILE ($splitkeywords2[$keyindex] != end($splitkeywords2));
  };

PS Where can I find info on the differences and uses of the different brackets - {}

Gentlemen,
Thanks for the solutions, all works perfectly.
I’m grateful to you.

Mike

() are also sometimes used for precedence in mathematical functions, as well as conditional statements (if, elseif) to encapsulate (and set precedence) for the conditional tests.


if ($theother && ($this || $that)) { do some stuff }

You are replacing the $result of the outer while in the do loop. Replace $result in the do loop with $result2 and it should work.



```php

$query  = "SELECT * FROM books";
$result = mysql_query($query)
      or die('mysql error ' . mysql_error() . ' in query ' . $query);
  while ($row = mysql_fetch_array($result))
  {
    $splitkeywords1 = $row['keywords'];
    $splitkeywords2 = explode(',',$splitkeywords1);
    $keyindex = -1;

    foreach($splitkeywords2 as $newtab)
    {
        $newtab = $splitkeywords2[$keyindex];
        echo $newtab;
        $query  = "SELECT * FROM $newtab";
        $result2 = mysql_query($query) // $result2 instead of $result
          or 
        tablecreation($newtab);
    }
  };

Or did you mean to overwrite $results ? I’m not entirely clear as to what the code should do.
Oh, I also replaced do {} while {} with a foreach()

As for the (), , and {}

  • () are used in funtion calls functionB[/B] and loop constructs foreach($var as $value)
  • are used for array indices $array[0] and substring $string[0]
  • {} are used for function and loop bodies: function foo($arg1,$arg2) { doSomething(); }, while($a < 10) {doSomething();}