PHP: "->"无法处理 mysql 结果集

标签 php mysql fetch

<分区>

我有一个奇怪的行为。直到今天我使用 -> 从 mysql 结果集中检索成员变量,例如

$username = $result->user_name

但这在我当前的项目中不起作用。

$sth = self::$dbConnection->prepare("SELECT user_id, user_name FROM users");                              
$sth->execute();
$result = $sth->fetch();

$test = $result->user_password_hash; // $test is null

$test = $result["user_password_hash"]; // this works

????

最佳答案

修复解析错误后,如果你想要一个对象:

$result = $sth->fetch(PDO::FETCH_OBJ);

或者:

$result = $sth->fetchObject();

或者在使用fetch()之前设置模式:

$sth->setFetchMode(PDO::FETCH_OBJ);

关于PHP: "->"无法处理 mysql 结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305033/

相关文章:

PHP MySQL max() 函数产生 5

php - 在 AWS Elastic Beanstalk 上安装 php mongo 驱动程序

mysql - 创建触发器以从一个数据库表插入到 PHPMYADMIN 中的不同数据库表中

mysql innodb : delete takes seconds

sql - fetching在计算机科学中是什么意思?

javascript - 获取多个 promise ,只返回一个

php - 3 月 14 日不是 86400 秒吗?

php - 奇怪的变量/方法/类名 - PHP

mysql - 提交到数据库后,输入字段保留空白并带有特殊字符

javascript - 如何根据回调的响应在 componentDidMount 中设置状态