2

我有一个PHP使用 SELECTSQL查询的函数。我在这样的查询中使用占位符变量(?)。(此占位符用于 mysql 数据库中的表名):

protected function _fetchPreviousShiftData($table, $report_time)
{

$query = "SELECT * FROM ? WHERE report_date=? and shift=?";
$previousShiftData = $this->_getDbConnection()->fetchAll($query,array($table, date("Y-m-d"), $this->_shiftValue($report_time, 8)));

return $previousShiftData;
}

但是我在 $table 变量中有一个错误,我应该如何使用“?” 对于 $table 变量?错误是这样的:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''adsl_support' WHERE report_date='2013-04-06' and shift='18-2'' at line 1' in C:\php_shared_lib\Zend\Db\Statement\Pdo.php:228
4

1 回答 1

2

您不能将其用于表名或字段名...仅用于传递的值...

您可以列出可能的表名,检查它并使用

"SELECT FROM `$table`..." 

如果检查通过

于 2013-04-06T06:28:56.213 回答