0

I have a field in a Mysql table which contains php variable, which is used to determine the directory where a file is stored (ex. Docs/$CompID/ and Acc/$AccID/). The problem is that when I retrieve the value, the php variable name is display instead of the value.

I tried placing quotes around the value -

Both in the Mysql table (Docs/$CompID/" and Docs/".$CompID."/)

and when the php variable is displayed (echo"$dir")

I also tried doing str_replace, putting { } around the variable so that I can identify it in the string:

$start=strrpos($cat['dir'],'{');
$stop=strrpos($cat['dir'],'}');
$rep=substr($cat['dir'],$start+1,$stop-1);
$dir=str_replace("{".$rep."}",$rep,$cat['dir']);

All of these just displayed the php variable name. Any help would be appreciated!

2
  • what does $cat['dir'] look like ? Commented Aug 22, 2012 at 5:14
  • Docs/$CompID/ and Acc/$AccID/ are examples of the type of data in $cat['dir']. When I was trying to use str_replace I changed them to Docs/{$CompID}/ and Acc/{$AccID}/... Commented Aug 22, 2012 at 5:17

2 Answers 2

2

You can use eval()

$AccID = 'me';
eval("\$str = \"Acc/$AccID/\";");
echo $str;
Sign up to request clarification or add additional context in comments.

3 Comments

This works if I save the variable into another one like this: $dir=$cat['dir']; eval("\$str = \"$dir\";"); echo $str; But not like this: eval("\$str = \"$cat['dir']\";"); echo $str; Any idea why?
Oh yah forgot my PHP quotes stuff. That's funny
Acc/$AccID/ is the value of $cat['dir']. Works like eval("\$str = \"$cat[dir]\";"); Thanks!!
1

If I am correct you variable would look like this: var. You can access $var with $$cat["dir"]. However, if you db variable looks like $var, just use a simple substring to remove the $.

2 Comments

I tried this, but it only worked if the php variable was the only thing in the Mysql field. When there is text along with the variable in the field, it to use the entire value as a variable.
This is really great to know, though!! I will definitely keep this in mind, though, when the entire Mysql field is a php variable!

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.