There are many approaches to this. If it is not important that the variable name matches the name column_name, you could assign an alias to the expression in the SELECT
For example:
SELECT whateverexpression AS mycol FROM mytable LIMIT 1;
Then you “know” the name of the variable in the $mycol object
echo $rs->mycol;
I think this approach may be good in the more general case when you are dealing with tables with column names that were assigned by syphilitic idiot developers who had a good reason for
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 many other approaches, for example, instead of PDO::FETCH_OBJ and using PDO::FETCH_NUM . If you want to stick with PDO::FETCH_OBJ , I think the alias assignment will work.
Getting metadata from a result set is an approach I would consider if getColumnMeta was not yet experimental.
spencer7593
source share