我正在尝试通过PHP和PDO FetchAll()以及Prepared Statements填充来自MySql表的一列的所有结果,但我的代码只返回一行而不是所有结果.我在这里看到了这个问题(PHP PDO返回单行),这与我的问题相似但我没有用这个问题改进我的代码.
我按照PHP手册页面(http://php.net/manual/en/pdostatement.fetchall.php)中的示例尝试将我的代码构造/调整为MySeql表中一列的fetchAll()结果:
PHP手册页 - 带print的FetchAll示例:
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
Run Code Online (Sandbox Code Playgroud)
我使用HTML select标签基于上面的示例调整了我的代码:
<select name="category" class="form-control" required>
<option value="0" selected="selected" disabled="disabled" style="display: none">Select one category</option>
<?php
require_once 'pdo_connection.php';
$sth = $dbh-> prepare( 'SELECT * FROM categories' );
$sth-> execute();
$result = $sth-> fetchAll(); …Run Code Online (Sandbox Code Playgroud)