Syntax error when binding table name as SQL parameter

38 Views Asked by At

my table is like below

CREATE TABLE IF NOT EXISTS BID_1s (
    secId varchar(6) NOT NULL UNIQUE, 
    fhigh    float,
    flow     float,
    fopen    float,
    fclose   float,
    fdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY(secId)
) ENGINE MEMORY;

and code is

$param = array(
    ":table" => $table, // $table = BID_1s
    ":secId" => $secId, //$secId = 4001
    ":fhigh" => $high, // $high = 22.65
    ":flow" => $low,   // $low = 22.00
    ":fopen" => $open, // $open = 22.21
    ":fclose" => $close, //$close = 22.34
    ":fdate" => $tempDate, // $tempDate = 2014-12-00 00:00:00
);

$query .= "INSERT INTO :table (`secId`, `fhigh`, `flow`, `fopen`, `fclose`, `fdate`) "
            . "VALUES (':secId', :fhigh, :flow, :fopen, :fclose, ':fdate') "
            . "ON DUPLICATE KEY UPDATE "
            . "fhigh=:fhigh, flow=:flow, fclose=:fclose, fopen=:fopen; \n";

$stmt = $this->dh->prepare($query);

foreach ($param as $param_key => $param_value) {
  $stmt->bindParam($param_key, $param_value);
}
if (!$stmt) {
  echo "\nPDO::errorInfo():\n";
  print_r($this -> dh -> errorInfo());
}
$exec = $stmt->execute();

when I run my program show syntax error:

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 ''2014-12-00 00:00:00' (secId, fhigh, flow, fopen, fclose, fdate) VAL' at line 1' in /var/www/html/storeSocketData/mysqlDriver.php:53

If you attention my $tempDate bind with :table and replace with it

Is this a Bug in PHP PDO or I can't bind table name in PDO ?

if I don't use bind and assign directly value in query, my query work Okey

0

There are 0 best solutions below