我有一个数据库表,用于存储要在动态查询中使用的值,该动态查询从存储在另一列中的表名中提取数据。例如,一个条目可能是:
selects | tableName
-----------------------------------------------------------------------
fName,lName,email,phone,address,city,state,zip | foobar
然后我接受这个值并用它来生成一个动态查询然后我用我的db类运行......就像这样:
$fields = $connection->runQuery("SELECT selectItems, tableName FROM foo;");
$query = "SELECT " . $fields[0]['selectItems'] . " FROM " . $fields[0]['tableName'] . ";";
$results = $connection->runQuery($query);
这很好,但是如果我尝试将选择值更改为这样的话:
selects | tableName
-----------------------------------------------------------------------------------------------
CONCAT(fName, ' ', lName) as fullName,email,phone,address,city,state,zip | foobar
我收到错误说:
未定义索引:CONCAT(fName,'',lName)为fullName
如何让它运行$ query变量,就像我自己键入它一样?
这是"选择"之一的C / P.值直接来自DB:
visitTimestamp as visitTS,firstName,lastName,CONCAT(apptDate,apptTime) as apptTS,address,phone,email,currentCarYear,currentCarMake,currentCarModel,currentCarValue,personID
当我回复$ query时,我显示了以下内容:
SELECT visitTimestamp as visitTS, firstName, lastName, CONCAT(apptDate, apptTime) as apptTS, address, phone, email, currentCarYear, currentCarMake, currentCarModel, currentCarValue, personID FROM foo
我复制并粘贴了回显的$ query字符串并通过phpMyAdmin运行它并且工作得很好。有什么想法为什么它作为变量执行时不会运行?