【问题标题】:PHP Dropdown List Causing Endless LoopPHP下拉列表导致无限循环
【发布时间】:2014-04-22 01:24:18
【问题描述】:

我正在尝试在 PHP 中动态填充下拉列表,这会导致无限循环并且我的浏览器崩溃。我不知道如何正确地让它显示一个表中的所有行,但我认为这将是一个相对简单的修复。 while 循环可能会把它扔掉。如果您需要更多信息,请告诉我,我正在关注此示例,但我的示例是用 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>

【问题讨论】:

    标签: php html mysql pdo while-loop


    【解决方案1】:

    改变

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

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

    这样$profilerow 不断变化并最终在没有更多行时在while 条件内评估为等效的FALSE。在您的版本中,$profilerow 成为一个对象并且永远不会改变。在while 条件下,对象的计算结果为TRUE

    【讨论】:

    • 谢谢!现在说得通了。
    【解决方案2】:

    $profileRow 没有变化。所以它永远是真的

    你需要这样做

    while($profileRow = $stmt-&gt;fetch())

    【讨论】:

    • 就是这样!谢谢!我会尽快将您的答案标记为正确的。
    猜你喜欢
    • 2016-05-31
    • 1970-01-01
    • 2018-04-15
    • 2020-08-27
    • 2021-05-07
    • 2012-08-24
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多