如何使用查询从数组中的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

我有数组值作为原因 p>

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

ie。,$ cause = 2,3,4 p>

如何从查询中获取该数组值的原因

我的表名是'cp_cause' p>

p>

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

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

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

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

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'];
}

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."'");
?>