我有两个表,table2有nameColumn
,其中包含table1列的名称,如
表1
id|check|status|...|
1 |abc |1 |...|
表2
|nameColumn|...|
|check |...|
|status |...|
我想在table1中获取值,列名是table2中的值,如
$sql="SELECT * FROM `table1` WHERE `id` = 1 ";
$result = mysqli_query($conn,$sql) or die (mysql_error ());
$rowTable = mysqli_fetch_array($result);
$s = "SELECT * FROM table2";
$r = mysqli_query($conn,$s);
while($row1 = mysqli_fetch_array($r)){
$columnName = $row1['nameColumn'];
$value = $rowTable["'".$columnName."'"]; // fail why?
//$value = $rowTable['check']; // working?
}
我不知道为什么$value = $rowTable["'".$columnName."'"];
失败错误,如
Notice: Undefined index: 'check' ...
但如果我使用像$value = $rowTable['check']; // working why?
如何解决这个问题
答案 0 :(得分:2)
失败了,因为你向它添加了不必要的'
字符。试试下面的代码
$value = $rowTable[$columnName];
答案 1 :(得分:0)
$value = $rowTable['".$columnName."'];