将一个变量放在php变量echo的旁边


puting a variable in side a php variable echo

这听起来可能很傻,也可能听起来不安全,但我在调用的poke1hp中一直到poke6hp都有列。我需要这样做,如果变量设置为1,它将选择并回显poke1hp列,如果变量为6,它将回显poker6hp

所以他是我得到的,应该有效,但不是。

if ($_SESSION['pickedslot'] == '1') {$tablename = "poke1hp";}
if ($_SESSION['pickedslot'] == '2') {$tablename = "poke2hp";}
if ($_SESSION['pickedslot'] == '3') {$tablename = "poke3hp";}
if ($_SESSION['pickedslot'] == '4') {$tablename = "poke4hp";}
if ($_SESSION['pickedslot'] == '5') {$tablename = "poke5hp";}
if ($_SESSION['pickedslot'] == '6') {$tablename = "poke6hp";}

我用$tablename做了一个选择,在这一点上,我回显了$tablename,一切都很好,列名就在那里。现在,我尝试在选择中使用上面设置的变量旁边的列名

// here we grab the users hp 
$grabhp = $db->prepare("select '$tablename' from new_battles WHERE username = ?");
$grabhp ->execute(array($_SESSION['username']));
$grabhp2 = $grabhp ->fetch(); 
echo $grabhp2 ['$tablename'];

但我什么都没打印出来。。。我知道我可以用行代替列,但我已经用这种方式编码了。。。

变量不在单引号内展开:

http://php.net/manual/en/language.types.string.php

我建议不要用引号作为你的例子。尝试:

echo $grabhp2[$tablename];

在您提供的示例中,您还错误地处理了MySQLi准备的语句。

这里有一个参考:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php