[Solved] what is it that i am doing wrong while converting [closed]


prepare() goes with execute()

Prepared statements basically work like this:

  1. Prepare: An SQL statement template is created and sent to the
    database. Certain values are left unspecified, called parameters
    (labeled “?”). Example:

    INSERT INTO mtTable VALUES(?, ?, ?)

  2. The database parses, compiles, and performs query optimization on
    the SQL statement template, and stores the result without executing
    it

  3. Execute: At a later time, the application binds the values to the
    parameters, and the database executes the statement. The application
    may execute the statement as many times as it wants with different
    values

try with below code

<?php

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

$rowperpage = 3;
$offset     = 0;

// counting total number of posts
$query = "SELECT count(id) AS allcount  FROM posts";
$stmt  = $db->query($query)->fetchColumn();

/******** The ABOVE QUERY LOOKS POINTLESS TO ME AS YOU NOT USING THE RESULTS FROM THAT QUERY*/

// select first 3 posts

$qry = "SELECT * FROM posts ORDER BY id ASC LIMIT ?,? ";
$stm = $db->prepare($qry);
$stm->execute(array($offset,$rowperpage));
$results = $stm->fetchall(PDO::FETCH_ASSOC);

if (count($results) > 0) {

    foreach ($results as $row) {

        $id           = $row['id'];
        $title        = $row['title'];
        $content      = $row['content'];
        $shortcontent = substr($content, 0, 160) . "...";
        $link         = $row['link'];

    }
} else {

    echo "No records found";
}
?>

5

solved what is it that i am doing wrong while converting [closed]