执行此功能时出错

时间:2012-11-19 05:55:55

标签: php codeigniter

  

可能重复:
  Message: Invalid argument supplied for foreach in CodeIgniter

Message: Invalid argument supplied for foreach in CodeIgniter继续。

这是$ datais来自的函数..

function getSearchedUniversityTab($country, $state, $level, $degType) {
      $query = $this->db->query("SELECT `university`.`uniId`, `university`.`name`
                                FROM (`university`)
                                inner JOIN (select degCollege, degType, count(*) as cnt  from degree where `degType` =  '$degType'  group by degCollege) clg
                                ON clg.`degCollege` = `university`.`uniId` 
                                WHERE `country` =  '$country'
                                AND `state` =  '$state'");
      $result = $query->result_array();

      foreach($result as $row)
      {
        $data[] = $row;
      }
      return $data;
      $this->db->close();  
   }

1 个答案:

答案 0 :(得分:0)

您的result最有可能返回空白 改为

function getSearchedUniversityTab($country, $state, $level, $degType) {
      $query = $this->db->query("SELECT `university`.`uniId`, `university`.`name`
                                FROM (`university`)
                                inner JOIN (select degCollege, degType, count(*) as cnt  from degree where `degType` =  '$degType'  group by degCollege) clg
                                ON clg.`degCollege` = `university`.`uniId` 
                                WHERE `country` =  '$country'
                                AND `state` =  '$state'");
      $result = $query->result_array();
     if(count($result) > 0 ) {
      foreach($result as $row)
      {
        $data[] = $row;
      }

      return $data;
     }else{
       return null;
     }
      $this->db->close();  
   }