user2793987
user2793987

Reputation: 199

PHP Dropdown List Causing Endless Loop

I am trying to dynamically populate a drop down list in PHP and it is causing an endless loop and my browser to crash. I do not know how to correctly get it to show all the rows in one table but I would think this would be a relatively simple fix. The while loop might be throwing it off. Let me know if you need more information I am following this example but mine is written in PDO:

Dynamic drop down list using html and php

<h3>Company Listing</h3>
           <select name='companies'>
            <option value="">--- Select ---</option>
          <?php
          //gets user's info based off of a username.
    $query = "SELECT business_name FROM Businesses";

    try 
    {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);

    }
    catch (PDOException $ex) {
    }

//fetching all the rows from the query
$profileRow = $stmt->fetch();
while ($profileRow) 
{ 
?>

<option value="<?php echo $profileRow['business_name']?>"><?php echo $profileRow['business_name']?></option>

<?php
}
?>
    </select>
          <p></p>

Upvotes: 1

Views: 144

Answers (2)

faintsignal
faintsignal

Reputation: 1836

Change

$profileRow = $stmt->fetch();
while ($profileRow) 
{ 

to

while ($profileRow = $stmt->fetch()) 
{ 

so that $profilerow keeps changing and eventually evaluates to something equivalent FALSE inside the while condition when there are no more rows left. In your version $profilerow becomes an object and never changes. An object evaluates to TRUE in the while condition.

Upvotes: 4

anurupr
anurupr

Reputation: 2344

$profileRow is not changing . so it will always be true

You need to do it like this

while($profileRow = $stmt->fetch())

Upvotes: 4

Related Questions