在mysql中使用php检索动态列卡住了


Stuck retrieving dynamic column in mysql using php

我有一个自定义字段,用户可以在其中插入文本,从而更改数据库列名。

我的一个页面功能是检索自定义列中的值。我现在面临的问题是,我无法检索值。为了您的信息,我已经创建了一个函数来获取列的名称。

在正常情况下,我像这样检索值。

 while($row = mysqli_fetch_assoc($result)) {
     $companys[$i]['check_existing_user'] = $row['id'];
 }

但是对于动态的,我尝试这样做,但它仍然不起作用。

  while($row = mysqli_fetch_assoc($result)) {
         $custom_field_1 = '''customized_field_name("custom_field_1")'''; // return the column name and look like this -> 'custom_field_1'
         $companys[$i]['custom_field_1'] = $row[$custom_field_1];
                        $i++;
  }

我觉得我直接插入变量的方式是错误的,但我不知道我还应该做什么。

如果它是一个PHP函数,那么你必须像调用PHP函数一样调用它

$companys[$i]['custom_field_1'] = $row[customized_field_name("custom_field_1")];

或者最好先将自定义名称存储在变量中,然后再使用

$custom_field_1 = customized_field_name("custom_field_1");
while($row = mysqli_fetch_assoc($result)) {
    $companys[]['custom_field_1'] = $row[$custom_field_1];
}