mysql如何获得字段值从数据库和传递给函数?

标签: MySQL PHP
发布时间: 2015/6/11 23:15:24
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我让两个功能,我想要从第一个函数 $id 获得 location_4 的值并将其值的数组传递给第二个功能怎么这样呢? 这是我的代码:

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:

试试这个:

 $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";
赞助商