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

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement