Page 1 of 1

Loop question

Posted: Fri Jan 23, 2004 9:27 am
by cjmcf529
I am converting a web site from ASP/VBScript to PHP it is driven by an MySQL Database. In asp I can do the following

Do Until rs.EOF
Print whatever code I want
rs.movenext
Loop

How do I create a loop like this in PHP. One that will read a Recordset from a query and output it as I see fit and then move to the next record until there are no records left.

Thanks

Posted: Fri Jan 23, 2004 9:32 am
by patrikG

Code: Select all

while(!$rs->EOF)
  {
  echo "my code";
  $rs->moveNext();
  }

Posted: Fri Jan 23, 2004 9:36 am
by twigletmac
You can do:

Code: Select all

// connect to db server, select db and then:
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result) {
  // output row
}
It'll depend on how you are querying the database (using abstraction or the mysql_xxx functions).

Mac

Posted: Fri Jan 23, 2004 10:08 am
by cjmcf529
This code seems to work partially:
while(!$rs->EOF)
{
echo "my code";
$rs->moveNext();
}



but it doesn't move to the next record. The error is:
Fatal error: Call to a member function on a non-object.

It returns the first record then that error

Posted: Fri Jan 23, 2004 10:20 am
by patrikG
Depends on which database wrapper you're using. The above code would work with adoDB. Just google it for a download.

Posted: Fri Jan 23, 2004 5:50 pm
by pickle
I would guess your problem stems from using $rs->moveNext(). Make sure that function exists on the $rs object. twigletmac's method is what I always use.