3

我一直在努力将我当前的脚本换成 PDO。我已经简化了这个例子的 MySQL 查询,但即使是这个版本,错误仍然存​​在。

$sql = 'SELECT * FROM :table WHERE lastUpdate > :appDate';

try{
    $db = connect();
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':table', $table);
    $stmt->bindParam(':appDate', $appDate);

    foreach($tablesToCheck as $table){
        $stmt->execute();
        $resultset[] = $stmt->fetchAll();
    }
} catch(PDOException $e){
    print 'Error!: '.$e->getMessage().'<br/>';
}//End try catch

$stmt->errorInfo() 返回:

( [0] => 42000 [1] => 1064 [2] => 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 ''GroupName' WHERE lastUpdate > NULL' at line 1 )
4

1 回答 1

1

这是在迈克尔的帮助下修改后的代码:

foreach($tablesToCheck as $table){
    $sql = 'SELECT *, \''.$table.'\' AS tableName FROM '.$table.' WHERE lastUpdate > :appDate';

    try{
        $db = connect();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':appDate', $appDate);
        $stmt->execute();

        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $resultset[] = $row;

    } catch(PDOException $e){
        print 'Error!: '.$e->getMessage().'<br/>';
    }//End try catch
}//End foreach
于 2012-12-04T23:02:27.417 回答