我试图通过
PHP和PDO FetchAll()与Prepared Statements填充来自MySql表的一列的所有结果,但我的代码只返回一行而不是所有结果.我在这里看到这个问题(
PHP PDO returning single row)与我的问题类似,但我没有通过这个问题改进我的代码.
我按照PHP手册页面(http://php.net/manual/en/pdostatement.fetchall.php)中的示例尝试将我的代码构造/调整为MySeql表中一列的fetchAll()结果:
PHP手册页 – 带print的FetchAll示例:
$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);
?>
我使用HTML select标签基于上面的示例调整了我的代码:
Select one category
require_once 'pdo_connection.php';
$sth = $dbh-> prepare( 'SELECT * FROM categories' );
$sth-> execute();
$result = $sth-> fetchAll(); ?>
而且,这是我的PDO数据库连接:
$dbh = new PDO('mysql:host=localhost;dbname=system_vip;charset=utf8', 'root', '');
?>
我可以做些什么来改进我的代码,使用预准备语句和PDO连接从MySql表上的一列填充所有结果?