Php 如何使用查询从数组中的id获取名称?
我有数组值作为原因 例如:$cause=$_请求['cause'] 即,$cause=2,3,4 如何从查询中获取该数组值 我的表名是“cp\u cause” 如何从上表中获取2,3,4原因名称 thinkphp中的示例查询模型是Php 如何使用查询从数组中的id获取名称?,php,mysql,thinkphp,Php,Mysql,Thinkphp,我有数组值作为原因 例如:$cause=$_请求['cause'] 即,$cause=2,3,4 如何从查询中获取该数组值 我的表名是“cp\u cause” 如何从上表中获取2,3,4原因名称 thinkphp中的示例查询模型是 $cause_name = $GLOBALS['db']->getAll("select category from ".DB_PREFIX."category where id = '".$cause."'"); 如果我没弄错的话,我想要劳工、健康、妇女的
$cause_name = $GLOBALS['db']->getAll("select category from ".DB_PREFIX."category where id = '".$cause."'");
如果我没弄错的话,我想要劳工、健康、妇女的名字:你得到逗号分隔的ID,想查询这个吗
SELECT * FROM cp_cause WHERE id IN (2, 3, 4)
PHP:
结果应该是一个包含匹配行的列表。但是我们不知道,getAll方法返回什么
示例:如果结果是数组,则可以迭代:
foreach($cpCauses as $cause) {
echo $cause['cause_name'];
}
如果我没弄错的话:你得到了逗号分隔的ID,想查询这个
SELECT * FROM cp_cause WHERE id IN (2, 3, 4)
PHP:
结果应该是一个包含匹配行的列表。但是我们不知道,getAll方法返回什么
示例:如果结果是数组,则可以迭代:
foreach($cpCauses as $cause) {
echo $cause['cause_name'];
}
您需要创建类似于
'2'、'3'、'4'
的字符串,以便在子句中使用MySql进行检查
例如
<?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."'");
?>
您需要创建类似于'2'、'3'、'4'
的字符串,以便在
子句中使用MySql进行检查
例如
<?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."'");
?>
你是在问如何做id在(2,3,4)中的位置?我有2,3,4,但我只想要id的名称像劳动、健康、女人你是在问如何做id在(2,3,4)中的位置?我有2,3,4,但我只想要id的名称像劳动、健康、女人一样,非常感谢@BenRoobThanks,谢谢@BenRoob