pdo mysql fetchall_我的PHP PDO fetchAll()代码返回一行而不是MySql列表上的所有结果

参见英文答案 > How to use PDO to fetch results array in PHP?                                    3个

我试图通过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表上的一列填充所有结果?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值