0

我有数组值作为原因

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

即,$cause = 2,3,4

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

我的表名是'cp_cause'

在此处输入图像描述

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

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

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

我想要劳工、健康、女性的名字

4

2 回答 2

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-Method 会返回什么!

示例:如果 result 是一个数组,则可以迭代:

foreach($cpCauses as $cause) {
    echo $cause['cause_name'];
}
于 2017-07-12T09:00:19.233 回答
1

您需要创建类似于使用 MySql子句'2','3','4'进行检查的字符串。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."'");
?>
于 2017-07-12T09:37:32.467 回答