1

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>

2 Answers 2

4

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

You need to do it like this

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's it! Thanks! I will mark your answer as the correct one as soon as I can.
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.