我有一个这样的数组:
$lang = array();
$lang['ITEM0'] = 'Home';
好了,如果我将变量名称$ lang ['ITEM0']保存到mysql中并尝试回显查询结果,它会将$ lang ['ITEM0']打印为字符串而不是变量,那么如何从mysql中打印变量名的正确值'Home'? 代码如下:
while($row = mysql_fetch_array($r))
{
echo $row['name'];
}
//result: $lang['ITEM0']
//I want result: Home
谢谢:)
答案 0 :(得分:1)
要保存不是变量,但只有索引才能获得更加理智的解决方案。
while($row = mysql_fetch_array($r))
{
echo $lang[$row['name']];
}
答案 1 :(得分:0)
上帝知道你为什么要在这样的数据库中存储变量,但我认为你的意思是你将行名称存储为字面$lang['ITEM0']
(不是该变量的内容,变量名称本身。)< / p>
但是,你要这样做:
eval("echo " . $row['name']);
但正如其他人所说,不要使用eval。我会重新考虑你是如何做到这一点的,因为它看起来像是一场等待发生的安全噩梦。
顺便说一句:如果你觉得你必须这样做,至少要使用“你的常识”的方法。答案 2 :(得分:0)
I came here is search of the same thing and later realized I already knew the answer was just searching to hard for something that was already there.
In your mysql statement save the name of your variable as something unique to you. Instead of $lang['ITEM0'] save it as {lang['ITEM0']} or any other unique. for example I have {userid}. However in your example you could then use in your php code: So now we'll assume
$lang = array();
$lang['ITEM0'] = 'Home';
while($row = mysql_fetch_array($r))
{
echo $row['name'] ;
}
Here you get output of: I want to go {lang['ITEM0']}.
Change it to this:
$lang = array();
$lang['ITEM0'] = 'Home';
while($row = mysql_fetch_array($r))
{
$var = str_replace('{lang['ITEM0']}', $lang['ITEM0'], $row['name']);
echo $var;
}
You will get: I want to go Home
Hope this helps anyone who comes to this page.
答案 3 :(得分:-1)
使用变量:
echo $$row['name'];
注意两个$
标志。
PS。切勿使用eval
;)