将数组中的值赋给codeigniter中的变量

时间:2015-03-05 14:49:37

标签: php codeigniter

我想将数组中的值保存到变量。进行if条件检查。这是我在models文件夹中的代码。我得到一个errormysqli_fetch_array()期望参数1是mysqli_result,给定对象。但是$ query返回一个数组值。

public function this_is_try(){
    $this->db->select('*');
    $this->db->from('partnerprofile');
    $this->db->where('User_Id','20'); 

    $query = $this->db->get();
    $query->result_array();

    $row = mysqli_fetch_array($query);
    $user_id = $row['User_Id'];
    $agefrom = $row['AgeFrom'];

    print $user_id;
    exit;
}

2 个答案:

答案 0 :(得分:1)

而不是传递整个$ results(结果对象)。将result_object存储到变量并传递它,即$ result = $ results [0];

public function this_is_try(){
$this->db->select('*');
     $this->db->from('partnerprofile');
     $this->db->where('User_Id','20');
     $query = $this->db->get();
     $results = $query->result();       
            $result = $results[0];
$user_id=$result->user_id;
$agefrom = $result->AgeFrom;

print $user_id;exit;

}

答案 1 :(得分:0)

试试这个:

public function this_is_try(){

$row = $this->db->select('*')
           ->from('partnerprofile');
           ->where('User_Id','20'); 
           ->get()->row();

$user_id = $row->User_Id;
$agefrom = $row->AgeFrom;

print $user_id;exit;

}