从数据库行获取所有数据?

时间:2014-08-13 21:48:12

标签: php mysql pdo

如何从数据库行获取所有数据?

目前这是我的查询:

try{
    $results = $db->query("SELECT name, id FROM players ORDER BY name ASC");
    $results->execute();
    // echo "Our query ran successfully.";
} catch (Exception $e){
    echo "Data could not be retrived from the database.";
    exit;
}

while ($row = $results->fetch()) {
    $joukkue    =   ($row["name"]);
    $id         =   ($row["id"]);
}

,这显示了一个数据库项:echo $name;

但我怎么能得到整排?

我的最终目标是将数据回显到表单选择标记示例:

<select id="select1" name="select">
    <option selected="selected" >Stuff here</option>
</select>

PS:我正在使用PDO连接到我的数据库。

1 个答案:

答案 0 :(得分:1)

SELECT * FROM players ORDER BY name ASC

<select id="select1" name="select">
  <?php
    while ($row = $results->fetch()) {
      echo '<option>'. $row["name"] .'</option>';
    }
  ?>
</select>