简体   繁体   English

如何从 PDO 将数据保存到 PHP 中的 cookie 中

[英]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?我需要一些帮助,因为已经在这里停留了一段时间,请作为一个菜鸟多多包涵,也许这对于经验丰富的开发人员来说可能很简单,基本上是在尝试将数据保存到 cookie 中,以便我只访问数据库一次,之后,我希望当我重新加载页面以从 cookie 中获取我的数据时,只需要再次访问数据库,我该如何实现呢? 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);

You could:你可以:

  1. Check if the cookie exists检查cookie是否存在
  2. If true, load data from it如果为真,则从中加载数据
  3. If false, load data from database and save cookie如果为 false,则从数据库中加载数据并保存 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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM