How to apply bindValue method in LIMIT clause?

后端 未结 10 2394
旧巷少年郎
旧巷少年郎 2020-11-21 04:28

Here is a snapshot of my code:

$fetchPictures = $PDO->prepare(\"SELECT * 
    FROM pictures 
    WHERE album = :albumId 
    ORDER BY id ASC 
    LIMIT :s         


        
10条回答
  •  醉话见心
    2020-11-21 05:09

    The simplest solution would be to switch the emulation mode off. You can do it by simply adding the following line

    $PDO->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    

    Also, this mode can be set as a constructor parameter when creating a PDO connection. It could be a better solution as some report their driver doesn't support the setAttribute() function.

    It will not only solve your problem with binding, but also let you send values directly into execute(), which will make your code dramatically shorter. Assuming the emulation mode has been already set, the whole affair will take as much as half a dozen lines of code

    $skip = isset($_GET['skip']) ? (int)trim($_GET['skip']) : 0;
    $sql  = "SELECT * FROM pictures WHERE album = ? ORDER BY id LIMIT ?, ?";
    $stmt  = $PDO->prepare($sql);
    $stmt->execute([$_GET['albumid'], $skip, $max]);
    $pictures = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

提交回复
热议问题