Skip to content
Advertisement

How to save data into cookie in PHP from PDO

I need some help as have been stuck here for a while, please bear with me as am a rookie and perhaps this might be simple to a seasoned developer, basically am trying to save data into a cookie so that i only hit the database once, after that, I want when i reload the page to get my data from the cookie only wihtout hitting the database again, how can i achieve that? below is my code so far

<?php

require_once("connection.php");

$sql = "select id,full_name,email,created_at from customers;";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();

setcookie("customerList",serialize($result),time() + 3600,"/","",0);

try{
    if($stmt->rowCount() > 0){
        echo"<h1>List of Customers</h1>";
        echo"<table>";
           echo"<tr>";
                echo"<th>S/N</th>";
                echo"<th>Full Name</th>";
                echo"<th>Email Address</th>";
                echo"<th>Created At</th>";
                echo"<th>Action</th>";
           echo"</tr>";
           
           foreach($result as $row){
            echo"<tr>";
               echo"<td>{$row['id']}</td>";
               echo"<td>{$row['full_name']}</td>";
               echo"<td>{$row['email']}</td>";
               echo"<td>{$row['created_at']}</td>";
               echo"<td><input type='button' value='View'></td>";
            echo"</tr>"; 
           }
        echo"</table>";
        //free result
        unset($result);
    }else{
        echo "No records from your query were returned";
    }

}catch(PDOException $e){
    die("ERROR: Could not be able to execute $sql." . $e->getMessage());
}
//close
unset($pdo);

Advertisement

Answer

You could:

  1. Check if the cookie exists
  2. If true, load data from it
  3. If false, load data from database and save cookie
  4. Loop over results and display

Here is an example:

$cookieName = 'customerList';

// 1. check if cookie exists
if (! empty($_COOKIE[$cookieName])) {
    // 2. get cookie data
    $result = unserialize($_COOKIE[$cookieName]);
}
else {
    // 3. there is no cookie, load data from database
    $sql = "select id,full_name,email,created_at from customers;";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $result = $stmt->fetchAll();
    // save in cookie
    setcookie($cookieName, serialize($result),time() + 3600,"/","",0);
}

// 4. display result using $result only
if (!empty($result)) {
    echo"<h1>List of Customers</h1>";
    echo"<table>";
       echo"<tr>";
            echo"<th>S/N</th>";
            echo"<th>Full Name</th>";
            echo"<th>Email Address</th>";
            echo"<th>Created At</th>";
            echo"<th>Action</th>";
       echo"</tr>";
       
       foreach($result as $row){
        echo"<tr>";
           echo"<td>{$row['id']}</td>";
           echo"<td>{$row['full_name']}</td>";
           echo"<td>{$row['email']}</td>";
           echo"<td>{$row['created_at']}</td>";
           echo"<td><input type='button' value='View'></td>";
        echo"</tr>"; 
       }
    echo"</table>";
    //free result
    unset($result);
} else{
    echo "No records from your query were returned";
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement