我有一个动态字段的查询,如何在不知道其名称的情况下访问此字段?
define('FIELD_NAME',"name"); $stmt = $connexion->query('SELECT '.FIELD_NAME.' from mytable); while ($rs=$stmt->fetch(PDO::FETCH_OBJ)){ echo $rs->FIELD_NAME; // DOESN'T WORK echo $rs->name; // WORK }
在{}中包装常量以创建动态变量.
echo $rs->{FIELD_NAME};
你可以从the documentation看到一些例子.
Curly braces may also be used,to clearly delimit the property name.