簡體   English   中英

在table1列中獲取值,列名是table2中的值

[英]Get value in a table1 column with the column name is value in table2

我有兩個表,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? $value = $rowTable['check']; // working why?

如何解決這個問題

它失敗了,因為你添加了不必要的' char。 嘗試下面的代碼

 $value = $rowTable[$columnName];
       $value = $rowTable['".$columnName."'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM