dongtiao0657 2014-03-18 13:57
浏览 18

将PDO与SELECT选项一起使用

I'm trying to use PDO for a SELECT Option, this is my functions.php code:

<?php
    class Benefits
    {
        public function fetch_all()
        {
            global $pdo;

            $benfitssql = $pdo->query('SELECT * FROM lkup_benefits');
            //$benfitssql->execute();

            while ($row = $benfitssql->fetch(PDO::FETCH_ASSOC))
            {
                $ops.= "<option value='" . $row['BenefitOption'] . "'>"
                    . $row['Benfits'] . "</option>";
            }
        }
    }
?>

This is my main page:

<label>Benefit Type</label>

<select class="select2_category form-control" data-placeholder="Choose a Category" tabindex="1" id="benefittype" name="benefittype">
    <?php echo $ops; ?>
</select>

When I select the SELECT Option, it opens with no records, why would this be, what have I missed?

  • 写回答

2条回答 默认 最新

  • dpevsxjn809817 2014-03-18 13:59
    关注

    There are two essential flaws with your code

    first, you should never format HTML inside of a function,
    second, you should return something from the function

    class Benefits {
        public function fetch_all() {
            global $pdo;
            return $pdo->query('SELECT * FROM lkup_benefits') ->fetchAll();
        }
    }
    ?>
    <label>Benefit Type</label>
    <select class="select2_category form-control" data-placeholder="Choose a Category" tabindex="1" id="benefittype" name="benefittype">
    <?php foreach ($benefits->fetch_all() as $row): ?>
        <option value="<?=$row['BenefitOption']?>"><?=$row['Benfits']?></option>
    <?php endforeach ?>
    </select>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失