Paging and error

I am trying to encode a small swap system, but as far as I understand, I am getting an error. Here is my code:

<!-- something before that working well -->    
else{
    include('head.php');
    if(empty($_GET['pg'])){ $_GET['pg'] = 0 ;}
    $offset = $_GET['pg'] * 5;
    $query = $db->prepare('SELECT * FROM posts ORDER BY id DESC LIMIT 5 OFFSET :n');
    $query->bindParam(':n', $offset);
    $query->execute();
?>
<body>
<?php 
    while ($data = $query->fetch()){
        echo '<article>'.$data['content'].'</article>';
       }}?>

   </body>

So I just want to display 5 articles per page. That is, I want the last 5 articles on the index page (i.e. page 0), then the next 5 articles on page 1, etc. So far, all I get is an error:

: "PDOException" "SQLSTATE [42000]: : 1064 SQL; , MySQL, '' 0 '' 1 '/Applications/MAMP/htdocs/index.php:24 : # 0/Applications/MAMP/htdocs/index.php(24): PDOStatement- > () # 1 {main} /Applications/MAMP/htdocs/index.php 24

24 - $query->execute();.

, , : ? , ?

+4
2

, sql 0.

'SELECT * FROM posts ORDER BY id DESC LIMIT 5 OFFSET: n'

'SELECT * FROM posts ORDER BY id DESC LIMIT 5 OFFSET "0" ' sql, ,

'SELECT * FROM posts ORDER BY id DESC LIMIT 5 OFFSET 0' - 0 ​​

$offset =  (int) ($_GET['pg'] * 5 ); // cast to an int so that you know its not a non-int value, then you don't need the protection of bind

$sql = 'SELECT * FROM posts ORDER BY id DESC LIMIT 5 OFFSET ' . $offset;

$query = $db->prepare($sql);

$query->execute();
+1

, ?

$offset = $_GET['pg'] * 5;
$query = $db->prepare("SELECT * FROM posts ORDER BY id DESC LIMIT 5 OFFSET ':n'");
$query->execute(array(':n' => $offset);

EDIT:

, .

$sql = "SELECT `password` FROM `users` WHERE `username` = :username";
$stmt = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$query = $stmt->execute(array(':username' => $username));
$rows = $query->fetchAll();
if (empty($rows)) {

}

.

0

All Articles