【问题标题】:assigning a value from a array to a variable in codeigniter将数组中的值分配给codeigniter中的变量
【发布时间】:2015-11-19 01:36:46
【问题描述】:

我想将数组中的值保存到变量中。用于进行 if 条件检查。这是我在模型文件夹中的代码。我得到一个错误mysqli_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;
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    而不是传递整个 $results(result object)。将 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;
    
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • 我在 C:\wamp\www\MilanRishtha\application\models\action_model.php 中遇到了语法错误语法错误,意外的 '->' (T_OBJECT_OPERATOR)
      • 尝试 $user_id = $row->User_Id; $agefrom = $row->AgeFrom;
      • 虽然这段代码 sn-p 可以解决问题,但代码外的including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多