开发者

mysql_fetch_object in a while loop and row->[attribute name]

开发者 https://www.devze.com 2023-02-15 08:28 出处:网络
I\'m very new to web programming, and I\'m working through a Vikram Vaswani\'s How to Do Everything With PHP and MySQL. Near the end, he shows how to do a web program which shows news items and allows

I'm very new to web programming, and I'm working through a Vikram Vaswani's How to Do Everything With PHP and MySQL. Near the end, he shows how to do a web program which shows news items and allows the user to edit and add different news items. The code doesn't seem to work and I've tweaked it to get it to work, but I'm still confused. In particular, I have the following code (this websit开发者_C百科e isn't handling the breaks very well)

// if records present
if (mysql_num_rows($result) > 0)
{
    // iterate through resultset
    // print article titles
    while($row == mysql_fetch_object($result));
    {
    ?>
        <li><a href="story.php?Ticker=<?php echo $row->Ticker;?>"><?php echo $row->StockName;?></a></li>
        <?php 
        $row = mysql_fetch_object($result);  
        echo $row->Ticker;
        echo $row->StockName;

<br>

Now, the book had while ($row = mysql_fetch_object($result)); - but that didn't do anything. So I changed it to an equal comparison operator. I'm thinking that with the $row = mysql_fetch_object($result) at the bottom in the loop, it should move onto the next row of the table since this is the normal behavior per http://us2.php.net/manual/en/function.mysql-fetch-object.php and per my own testing. I can do this differently with a for loop or something but I would like to figure out why it isn't working how I expect here.


It should definitely be:

while($row = mysql_fetch_object($result))
{

}

It looks like you've got a semicolon at the end of the while line, and that would end the while loop right then. Instead you should be opening up a block of code with parenthesis. If no results are returned, then it will be false and exit the while loop.


In your while loop, the mysql_fetch_object() returns the next row in your $result. If you're testing with == operator it cannot be true.

0

精彩评论

暂无评论...
验证码 换一张
取 消