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

时间:2014-02-18 15:34:13

标签: php mysql function mysqli

我创建两个函数我想从第一个函数获取location_4值作为$ id并将其values数组传递给第二个函数我是如何做到的?这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

试试这个:

 $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";