【问题标题】:how to get field value from database and pass to function?如何从数据库中获取字段值并传递给函数?
【发布时间】:2014-03-18 11:23:39
【问题描述】:

我创建了两个函数,我想从第一个函数中获取 location_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']; 
    }
}

【问题讨论】:

    标签: php mysql function mysqli


    【解决方案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";
    

    【讨论】:

    • 这不是我的问题,我的问题是获取 location_4 字段的数组并作为 $id 传递给函数 getCityName($id)
    • 您是否至少尝试过代码...我认为这是一个非常好的方法??
    • location_4 表在另一个数据库中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 2017-02-23
    相关资源
    最近更新 更多