1

I want to get rows from MySQL Database and save two specific values(row[0] & row[6]) in $_SESSION variable for comparison later on. The code looks like below:

        $sqlResult =mysql_query("SELECT * FROM questionbank WHERE quizName ='$quizNames' ORDER BY RAND() limit 6")

    while ($row= mysql_fetch_array($sqlResult)) 
            {
                $questionPkId = $row[0];                    
                echo $_SESSION['$questionPkId'] = $row[6];

            }

The Problem is I can't get this values in other pages. Can I make an associative array with session variable (though i tried array_push(), But it did not work).

6
  • 2
    Did you make sure to call session_start on every page that uses $_SESSION ? Commented Nov 18, 2013 at 21:13
  • 2
    Don't use mysql_* functions. They are deprecated. Also, a tip, don't SELECT * but write out the columns you need. Commented Nov 18, 2013 at 21:15
  • @Brad F : yes I did start session in all pages Commented Nov 18, 2013 at 21:17
  • 2
    Learn basic PHP syntax. '$questionPkID' is not going to evaluate to what you think it is... Commented Nov 18, 2013 at 21:18
  • @BartFriederichs Instead of mysql_* what should I use? Also I will need the row values later on. because I am planning to store values in one array and another one to display my questions and finally compare them to get the correct answer. Please advise (Thanks in advance) Commented Nov 18, 2013 at 21:19

2 Answers 2

2

A couple issues with your code. First up, make sure you call session_start on any page that uses sessions. Without it, you will not be able to access the variables previously set.

Second up, when a variable is called inside single quotes it is taken literally, meaning your session define statement is always assigning the variable to same thing, and hence overwriting what it should be.

$_SESSION[$questionPkId] = $row[6];

And that will assign the value to the actual value of $questionPkId. Aside from those issues, your session items should work, and if they are not you will need to provide us with more of your code.

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

Comments

0

Try without single quotes:

$_SESSION[$questionPkId] = $row[6];

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.