0

If I have an array:

$resultArr = pg_fetch_array($result,NULL);

and at the top of my php code I declare:

$_SESSION['resultArr'] = $resultArr;

Why can't I access the array elements like so:

for($i = 0; $i < $NUM_COLUMNS; $i++){
    // creation of the table and row are handled elsewhere.
    // The table is also within a <form> if that matters
    echo "<td>" .$_SESSION['resultArr'][$i]."</td>";
}

My table ends up having empty columns and I can't figure out why...

EDIT: I figured it out. I was declaring $_SESSION['resultArr'] = $resultArr; at the top of my code (right after session_start()) and it wasn't getting set. I moved it down to the point right after $resultArr = pg_fetch_array($result,NULL);

Is this how it's supposed to work or should it have worked fine at the top of the code?

6
  • Are you initialising the session properly? What does var_dump($_SESSION['resultArr']); output? Commented May 6, 2013 at 23:00
  • 1
    What is $NUM_COLUMNS set to? Also, are you remembering to start the session? Commented May 6, 2013 at 23:00
  • There is no reason this shouldn't work. Please do var_dump($_SESSION['resultArr']) to find out what's inside. Commented May 6, 2013 at 23:00
  • 2
    I think that $NUM_COLUMNS is your problem Commented May 6, 2013 at 23:04
  • 1
    Well, you just answered your own question. $_SESSION['resultsArr'] is null. Commented May 6, 2013 at 23:18

2 Answers 2

1

Maybe you did and didn't mention, but you must call session_start() before any operation on $_SESSION

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, session_start() is there at the top before anything
0

after your edit, yes this is how it is supposed to work, you first need to declare $resultArr, then put it's value in the session array

beacause in php you are no longer working with pointers, $_SESSION['resultArr'] = $resultArr; mean "$_SESSION['resultArr'] takes all the values of $resultArr at that precise moment", but it does not mean "they are thesame, if one changes, then the other changes too" .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.