How to assign a session value to a variable

I have a session which I need to assign to a variable to be used on Stored Procedure. But cant seem to get the variable being affected on it

I have already tried the following, but nothing


    echo $_SESSION['User'];
    $userV = ob_get_contents();


    $userV =  $_SESSION['User'];


    $userV= echo $_SESSION['User']; //fails as echo, used print instead


    $userV= print $_SESSION['User'];

But when I use a know value like below it works

   $userV= 41;

The intended Procedure as follows:

    $sql="CALL sp_getUser('$userV')";

1 answer

  • answered 2018-10-19 23:16 jeppeb

    The second one is the correct one: $userV = $_SESSION['User'];. If $userV does not hold the expected value after this assignment, two things can be the reason: You haven't initialized the session (session_start()) or the you haven't set the $_SESSION["User"] (somewhere in another script you should have something like $_SESSION["User"]=41;.

    You can test what is in the $_SESSION-variable presently by print_r($_SESSION);