Prince
Prince

Reputation: 1192

Prevent infinite loop issue in php

I am working on a project and I have two portions of code:

Wish.class.php

public function getWishes(){
        return $this->database->query('SELECT * FROM wishes');
    }

index.php

<?php
        while ($wishes = $wish->getWishes()->fetch()) {
            ?>
            <div class="row">
                <div class="col-md-4">
                    <h2><?=$wishes->title;?></h2>
                    <p><?=$wishes->wish;?></p>
                    <p><a class="btn btn-default" href="#">View details</a></p>
                </div>
            </div>
            <?php
        }
    ?>

When I run ìndex.php, I am able to get $wishes->title and $wishes->wish. The var_dump($wishes) fives me the following result: Var_dump

The problem I am facing is that, on the index.php the while generate an infinite loop and I don't really know why. Kindly help me solve this problem.

Upvotes: 0

Views: 623

Answers (1)

kashmira paghdal
kashmira paghdal

Reputation: 26

Try this

Please replace your code in index.php (Line no. 34 to 49) with below code.

<?php
    $wishes = $wish->getWishes()->fetchAll();
    foreach ($wishes as $wish) {
        ?>
        <div class="row">
            <div class="col-md-4">
                <h2><?= $wish->title; ?></h2>
                <p><?= $wish->wish; ?></p>
                <p><a class="btn btn-default" href="#">View details</a></p>
            </div>
        </div>
        <?
    }
    ?>

Please let me know if it works for you.

Upvotes: 1

Related Questions