duanmao2650 2012-11-27 21:33
浏览 66
已采纳

42000执行预准备语句时查询中出现语法错误

I have been pulling my hair out trying to swap my current script over to PDO. I have simplified the MySQL query for this example, but the error remains even with this version.

$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() returns:

( [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 )
  • 写回答

1条回答 默认 最新

  • douchuo9476 2012-12-04 23:02
    关注

    Here is the revised code with Michael's help:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)