SeeJay
SeeJay

Reputation: 25

How to store all data in database using session and retrieve it to use as ID in PHP

I want to store all the ID into session let's just say i have 2 data in my row which is ID 1 and 2 now I want to store that two ID so that I can call it later.

if ($Reservation_Result->num_rows > 0) 
    {
        while($row = mysqli_fetch_array($queryReservation))
         {
         $RID = $row['Reservation_ID'];
         } 
    }
 echo $RID; // and this will show the ID 1 and 2

now how can i store it in session like $ID1 = $_SESSION['Reservation_ID'] and $ID2 = $_SESSION['Reservation_ID'] so that I can call $ID1 and $ID2 to run some queries later?

Upvotes: 1

Views: 470

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

1.Add session_start(); on top of the code just after starting<?php so that you can use SESSION on the page.

2.Now change code like below:-

if ($Reservation_Result->num_rows > 0) 
{
    while($row = mysqli_fetch_array($queryReservation))
     {
      $_SESSION['reservation_ids'][]= $row['Reservation_ID'];
     } 
}
print_r($_SESSION['reservation_ids']);//to check that it's created and have values in it.

3.To use it on other pages also you need to have session_start(); on those page too (on top). And remember it's an array so treat it like an array not an string variable.

4.To get values from the array:-

foreach($_SESSION['reservation_ids'] as $value){
 echo "ID : $value \n";
}

Upvotes: 1

Related Questions