dongsun5330
dongsun5330
2017-07-12 08:56
浏览 327
已采纳

如何使用查询从数组中的id获取名称?

I had array values as cause

Ex: $cause = $_REQUEST['cause'];

ie., $cause = 2,3,4

How to get that array value cause name from query

My table name is 'cp_cause'

enter image description here

How to get the 2,3,4 cause name from the above table.

My sample query model in thinkphp is

$cause_name = $GLOBALS['db']->getAll("select category from ".DB_PREFIX."category where id = '".$cause."'");

i want the name of labour, health, women

图片转代码服务由CSDN问答提供 功能建议

我有数组值作为原因

Ex:$ cause = $ _REQUEST ['cause'];

ie。,$ cause = 2,3,4

如何从查询中获取该数组值的原因< / p>

我的表名是'cp_cause'

如何从上表中获取2,3,4原因名称。

我在 thinkphp 中的示例查询模型是

  $ cause_name = $ GLOBALS ['db']  - &gt; getAll(“select category from”.DB_PREFIX。“category where id ='”。$ cause。“'”); 
   
 
 

我想要劳动,健康,女性的名字

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongrong7267
    dongrong7267 2017-07-12 09:00
    已采纳

    If I get it right: you get comma separated Ids and want to query this?

    SELECT * FROM cp_cause WHERE id IN (2, 3, 4)
    

    PHP:

    $cpCauses = $GLOBALS['db']->getAll("select * from cp_cause where id in('".$cause."')");
    

    The result should be a list, containing the matching rows. But we do not know, what your getAll-Method returns!

    Example: if result is an array, you can iterate:

    foreach($cpCauses as $cause) {
        echo $cause['cause_name'];
    }
    
    点赞 评论
  • dt4320279
    dt4320279 2017-07-12 09:37

    You need to create string like '2','3','4' for checking with MySql in clause.

    For e.g.

    <?php
        $cause = array();
        $cause[] = '2';
        $cause[] = '3';
        $cause[] = '4';
        $sql_where = array(); 
        foreach($cause as $values){
            $sql_where[] = "'".$values."'";
        }
        $sql_where = implode(",",$sql_where);
        $cause_name = $GLOBALS['db']->getAll("select category from ".DB_PREFIX."category where id in '".$sql_where."'");
    ?>
    
    点赞 评论

相关推荐