2017-07-12 47 views
0

我有數組值作爲原因如何從陣列中使用ID查詢中獲取的名字嗎?

例如:$ cause = $ _REQUEST ['cause'];

即,$原因= 2,3,4

如何從查詢中獲取數組值的原因名

我的表名是 'cp_cause'

enter image description here

如何從上表中獲得2,3,4名的原因。

thinkphp我的樣本查詢模型

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

我想勞動,健康的名義,女性

+2

你在問怎麼做'WHERE id IN(2,3,4)'? – Kulvar

+0

我有2,3,4,但我想,IDS命名只喜歡勞動,衛生,婦女 – JoygiveKalai

回答

2

如果我得到它的權利:你逗號分隔的ID,想要查詢該?

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

PHP:

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

結果應該是一個列表,包含匹配的行。但是,我們不知道,你的GETALL法的回報!

例如:如果結果是一個數組,你可以遍歷:

foreach($cpCauses as $cause) { 
    echo $cause['cause_name']; 
} 
+0

它顯示了一個名字只 – JoygiveKalai

+0

與例如更新的答案 – BenRoob

+0

非常感謝,謝謝@BenRoob – JoygiveKalai

1

你需要像'2','3','4'創建字符串與MySQL in條款檢查。

例如,

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