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 に答える