我在PHP
中有一个函数,使用SELECT SQL
查询。我在查询中使用占位符变量(?)(此占位符用于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;
}
,但我有一个错误在$表变量,我应该如何使用"?"$表变量?错误如下:
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_libZendDbStatementPdo.php:228
不能用于表名或字段名…
您可以列出可能的表名,检查它并使用
构建查询"SELECT FROM `$table`..."
如果检查通过