Php 如何从数据库中获取字段值并传递给函数?

Php 如何从数据库中获取字段值并传递给函数?,php,mysql,function,mysqli,Php,Mysql,Function,Mysqli,我创建了两个函数,我想从第一个函数中获取位置_4值作为$id,并将其值数组传递给第二个函数。我是如何做到这一点的?下面是我的代码: function getbussinessdetail($profit_id) { $select="select a.account_id,a.title,a.budget,a.location_4,a.renewal_date,l.name from listing a,listinglevel l where l.value=a.level and p

我创建了两个函数,我想从第一个函数中获取位置_4值作为$id,并将其值数组传递给第二个函数。我是如何做到这一点的?下面是我的代码:

function getbussinessdetail($profit_id)
{
    $select="select a.account_id,a.title,a.budget,a.location_4,a.renewal_date,l.name from listing a,listinglevel l where l.value=a.level and profit_instructor_id='".$profit_id."' group by a.account_id";
    $result = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
    while($rs=$result->fetch_assoc ())
    {
            $res[]=$rs;
    }       
    return $res;
}   
function getCityName($id)
{
    if($id>0)
    {
        $select="select name from Location_4 where id=$id";
        $result = $GLOBALS ['mysqli1']->query ($select) or die ($GLOBALS ['mysqli1']->error . __LINE__);
        $rs=$result->fetch_assoc ();

        return $rs['name']; 
    }
}
试试这个:

 $profit_id = 4;
 $res = array();
 $res = getbussinessdetail($profit_id);

 if(count($res) > 0)
 {
   $cityNames = array();
   foreach($res as $id)
   {
    $cityNames[] = getCityNames($id['location_4']);
   }

 }

 var_dump($cityNames);
或者,只需更新第一个查询并删除第二个函数:

 $select="select a.account_id,a.title,a.budget,a.location_4,a.renewal_date,
          l.name from listing a,listinglevel l where l.value=a.level 
          and profit_instructor_id='".$profit_id."' group by a.account_id";
关于这一点:

 $select= "select a.account_id,a.title,a.budget,
      (select name from Location_4 where id=a.location_4) as cityName,
       a.renewal_date, l.name 
      from listing a,listinglevel l 
      where l.value=a.level 
      and profit_instructor_id='".$profit_id."' group by a.account_id";

这不是我的问题,我的问题是获取location_4字段的数组,并将其作为$id传递给函数getCityName$id您是否至少尝试了该代码…我认为这是另一个数据库中非常好的一个aproach??location_4表