0

我有两个表,table2nameColumn包含 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?

怎么解决 谢谢

4

2 回答 2

2

它失败了,因为您向其中添加了不必要的'字符。试试下面的代码

 $value = $rowTable[$columnName];
于 2013-08-15T04:29:18.377 回答
0
       $value = $rowTable['".$columnName."'];
于 2013-08-15T05:34:49.947 回答