Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store an array into a session variable in php

//Returns 10 question from questions table

$result = mysqli_query($con,"SELECT question FROM questions ORDER BY rand() LIMIT 10' ");
while($row = mysqli_fetch_row($result))
{
$que[]=$row[0];
}  

Now I need to store this whole set of $que[] in a session variable. (i.e 10 questions)

Something like this

$_SESSION['question'] = $que[]; 

$my_array[] = $_SESSION['question'];

so that $my_array[0] returns first question, $my_array[1] returns second questions and like that.

(Thanx for the help in advance)

like image 797
Aayush Avatar asked Aug 28 '13 22:08

Aayush


People also ask

Can we save array in session?

Saving arrays to localStorage and sessionStorage In this example, we have an array with numbers: var ourArray =[1,2,3,4,5]; We can now save it to localStorage or sessionStorage using the setItem() method: localStorage.

How can we store variable in session in PHP?

Starting a PHP SessionSession variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.

Can you store an object in a session variable?

Yes, You can save the class objects/instance in session and access at other pages.

Why session_start () is used in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.


1 Answers

Assign

$_SESSION['question'] = $que; 

print_r($_SESSION['question'][0]); will give you first question.

like image 170
Prasanth Bendra Avatar answered Sep 22 '22 12:09

Prasanth Bendra