0

I have this $res variable that gets a result from a DB query. The result contains the name of a session variable in a string. That session variable is already set and has the value of 5 (INTIGER), for example:

$res = mysql_query('SELECT `result` from `table`....');
....
gives the $res variable this string value: "Result: $_SESSION[myVar]";
print_r($res); // outputs Result: $_SESSION[myVar] and not "Result: 5"

How can I parse the string to get the value of the session variable?

Alin B.
  • 3
  • 1
  • 6
  • You need to use concatenation or simply wrap the $_SESSION[$yourVar'] with {} – RohitS Dec 02 '18 at 14:50
  • @RohitS can you give me an example, please? I query a database and the string that I get contains the name of the variable. It already has a value, I just need to transform that name into the value: $_SESSION[myVar] into 5, or whatever that value is. Tks! – Alin B. Dec 02 '18 at 15:17
  • Sure.. lets say you query database and get "Index" in some value like $res. Now the $res is like `$res = "index"` Now simply to use it for echoing content from session value like `$_SESSION[$res]`. As the string "index" in your case it might be "5" is retrieved in variable you have to use it. – RohitS Dec 02 '18 at 15:35
  • if you are looking out for string concatenation you have to use `.` (Period) operator and when you directly quote your PHP variable into string you need to simply wrap them in {} – RohitS Dec 02 '18 at 15:37
  • @RohitS, tks much! That is a solution to get just the name in the $_SESSION. But I have different situations, where the variable could be $_GET or $_POST. That's why I get the whole "$_SESSION[....]" and parse it into its value. The whole case is quite complex, even if it might look stupid to get reference to those types of vars from database. – Alin B. Dec 02 '18 at 15:53
  • ok. looks like i didn't got the root of and looks like you were looking out for getting some PHP variable from database and then use it..anyways you have your solution... cheers! – RohitS Dec 04 '18 at 16:14
  • @RohitS, tks much! I finally ended up with this solution: `preg_match("/$_(.*?)\[(.*?)\](?:\[(.*?)\])?/",$VAR,$matches);`where i get an array and retrieve the case of variable ($_SESSION, $_POST, $_SERVER etc.) and its corresponding key. – Alin B. Dec 06 '18 at 10:42

3 Answers3

1

Use string concatenation:

$res = "Result: ".$_SESSION[myVar];
print_r($res);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Abhishek
  • 382
  • 1
  • 6
  • what is myvar? either it should be wrapped in quotes or need to be literal! – RohitS Dec 02 '18 at 15:00
  • The $res gets that string from a database query. And it gets it as such. It works if I EVAL() that string, but I want to avoid using that solution. – Alin B. Dec 02 '18 at 15:11
  • I'd suggest you to use: mysql_fetch_assoc and get help from documentation: http://php.net/manual/en/function.mysql-query.php – Abhishek Dec 02 '18 at 15:42
  • @Abhishek thanks for the tip but I already get the $res using fetch_assoc. Is there another way to get the value of the $_SESSION[...] variable contained into that result other than using EVAL()? – Alin B. Dec 02 '18 at 15:57
0

You need to start a session and echo the variable.

<?php
session_start();
$myvar=$_SESSION["myVar"];
echo "Result: ".$myvar;
?>

Edit: What you can do is:

preg_match_all("/\[([^\]]*)\]/",$res,$matches);
$var=matches[1];
$session=$_SESSION[$var];
echo $session;

This finds the var used in the string for the session. Then it inputs it into the $_SESSION to get that session variable.

Maciek Semik
  • 1,872
  • 23
  • 43
0

should read $res = "Result:" . $_SESSION[myVar];