Loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjmcf529
Forum Newbie
Posts: 4
Joined: Fri Jan 23, 2004 9:27 am

Loop question

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

while(!$rs->EOF)
  {
  echo "my code";
  $rs->moveNext();
  }
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
cjmcf529
Forum Newbie
Posts: 4
Joined: Fri Jan 23, 2004 9:27 am

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Depends on which database wrapper you're using. The above code would work with adoDB. Just google it for a download.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply