Mysql PDO:在查询中获取带有“动态”字段的行

时间:2022-09-18 12:57:04

I have a query with a dynamic field, how do I access this field without knowing its name?

我有一个动态字段的查询,如何在不知道其名称的情况下访问此字段?

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
}

4 个解决方案

#1


3  

Wrap the constant in {} to create dynamic variables.

在{}中包装常量以创建动态变量。

echo $rs->{FIELD_NAME};

You can see some example from the documentation.

您可以从文档中看到一些示例。

Curly braces may also be used, to clearly delimit the property name.

也可以使用卷曲括号来清楚地界定属性名称。

Demo: http://3v4l.org/sgvV4

#2


3  

There are lot of approaches to this. If it's not important that the variable name match the column_name, you could assign an alias to the expression in the SELECT statement

有很多方法可以解决这个问题。如果变量名称与column_name匹配并不重要,则可以在SELECT语句中为表达式分配别名

For example:

 SELECT whateverexpression AS mycol FROM mytable LIMIT 1;

Then, you'd "know" the name of the variable in the object is $mycol

然后,你“知道”对象中变量的名称是$ mycol

    echo $rs->mycol;

I'm thinking this approach might be good in the more general case, when you're dealing with tables that have column names that were assigned by syphilitic idiot developers who had a good reason to

我认为这种方法在更一般的情况下可能是好的,当你处理的是具有由梅毒笨蛋开发者分配的列名的表时,他们有充分的理由

CREATE TABLE t (`Hey!$I (can)^name.\my->column Wh@tever` INT);
INSERT INTO t VALUES (42);
SELECT `Hey!$I (can)^name.\my->column Wh@tever` FROM t;

Obviously, there are lots of other approaches, like avoiding PDO::FETCH_OBJ and using PDO::FETCH_NUM instead. If you want to stick with PDO::FETCH_OBJ, I'm thinking assigning an alias would be workable.

显然,还有很多其他方法,比如避免使用PDO :: FETCH_OBJ和使用PDO :: FETCH_NUM。如果你想坚持使用PDO :: FETCH_OBJ,我认为分配一个别名是可行的。

Retrieving the metadata from the resultset is an approach I would consider, if getColumnMeta wasn't still experimental.

如果getColumnMeta还没有实验,那么从结果集中检索元数据是我会考虑的一种方法。

#3


1  

define('FIELD_NAME',"name");
        $stmt = $connexion->query('SELECT '.FIELD_NAME.' from mytable');   

        while ($rs=$stmt->fetch(PDO::FETCH_NUM)){
            echo $rs[0];
        }

Using this approach, if FIELD_NAME is defined to something like *, you will still be able to get the first column.

使用这种方法,如果将FIELD_NAME定义为类似*,您仍然可以获得第一列。

#4


0  

If you're only trying to get a single value from each row, you can use PDOStatement::fetchColumn()

如果您只想从每一行获取单个值,则可以使用PDOStatement :: fetchColumn()

define('FIELD_NAME',"name");
$stmt = $connexion->query('SELECT '.FIELD_NAME.' from mytable');
while ($col = $stmt->fetchColumn()) {
    echo $col;
}

Also note, you were missing a '.

还要注意,你错过了'。

#1


3  

Wrap the constant in {} to create dynamic variables.

在{}中包装常量以创建动态变量。

echo $rs->{FIELD_NAME};

You can see some example from the documentation.

您可以从文档中看到一些示例。

Curly braces may also be used, to clearly delimit the property name.

也可以使用卷曲括号来清楚地界定属性名称。

Demo: http://3v4l.org/sgvV4

#2


3  

There are lot of approaches to this. If it's not important that the variable name match the column_name, you could assign an alias to the expression in the SELECT statement

有很多方法可以解决这个问题。如果变量名称与column_name匹配并不重要,则可以在SELECT语句中为表达式分配别名

For example:

 SELECT whateverexpression AS mycol FROM mytable LIMIT 1;

Then, you'd "know" the name of the variable in the object is $mycol

然后,你“知道”对象中变量的名称是$ mycol

    echo $rs->mycol;

I'm thinking this approach might be good in the more general case, when you're dealing with tables that have column names that were assigned by syphilitic idiot developers who had a good reason to

我认为这种方法在更一般的情况下可能是好的,当你处理的是具有由梅毒笨蛋开发者分配的列名的表时,他们有充分的理由

CREATE TABLE t (`Hey!$I (can)^name.\my->column Wh@tever` INT);
INSERT INTO t VALUES (42);
SELECT `Hey!$I (can)^name.\my->column Wh@tever` FROM t;

Obviously, there are lots of other approaches, like avoiding PDO::FETCH_OBJ and using PDO::FETCH_NUM instead. If you want to stick with PDO::FETCH_OBJ, I'm thinking assigning an alias would be workable.

显然,还有很多其他方法,比如避免使用PDO :: FETCH_OBJ和使用PDO :: FETCH_NUM。如果你想坚持使用PDO :: FETCH_OBJ,我认为分配一个别名是可行的。

Retrieving the metadata from the resultset is an approach I would consider, if getColumnMeta wasn't still experimental.

如果getColumnMeta还没有实验,那么从结果集中检索元数据是我会考虑的一种方法。

#3


1  

define('FIELD_NAME',"name");
        $stmt = $connexion->query('SELECT '.FIELD_NAME.' from mytable');   

        while ($rs=$stmt->fetch(PDO::FETCH_NUM)){
            echo $rs[0];
        }

Using this approach, if FIELD_NAME is defined to something like *, you will still be able to get the first column.

使用这种方法,如果将FIELD_NAME定义为类似*,您仍然可以获得第一列。

#4


0  

If you're only trying to get a single value from each row, you can use PDOStatement::fetchColumn()

如果您只想从每一行获取单个值,则可以使用PDOStatement :: fetchColumn()

define('FIELD_NAME',"name");
$stmt = $connexion->query('SELECT '.FIELD_NAME.' from mytable');
while ($col = $stmt->fetchColumn()) {
    echo $col;
}

Also note, you were missing a '.

还要注意,你错过了'。