【问题标题】:Variable for the field name of mysql row resultmysql 行结果字段名的变量
【发布时间】:2012-09-21 21:35:35
【问题描述】:

我正在用 php 从 mysql 中检索一行,它有如下字段:name_en、name_es、name_de...

我想根据我的 $lang 变量(en、es、de...)检索正确的字段。如果 $lang 变量是 'es',我需要得到 $row['name_es']。

我已经尝试过(基于this thread),但它不起作用:

$name = $row->{'name_'.$lang};

知道如何使用变量作为行字段的名称吗?

提前感谢您的帮助!

【问题讨论】:

    标签: php mysql variables field


    【解决方案1】:

    你有吗? $row['name_es']

    试试看:

    $name = $row['name_'.$lang];
    

    【讨论】:

    • 成功了!我不知道我以前怎么没有尝试过...谢谢!
    【解决方案2】:

    如果您在$row 中有对象,请尝试此操作

    $field = 'name_' . $lang;
    $name = $row->$field;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多