活跃记录codeigniter

时间:2017-11-21 15:11:05

标签: php sql codeigniter activerecord

如何在codeigniter中将此代码更改为活动记录?

$q = "SELECT u.token
  FROM user u 
  WHERE u.userid = ?";
  $r = $this->db->query($q, [$id]);

$this->db->close();

return count($r->result_object) == 1 ? $r->row()->token : 0;

2 个答案:

答案 0 :(得分:1)

您可以针对您的问题尝试此解决方案:

function get_count($id){
   $this->db->select('u.token');
   $this->from('user u');
   $this->where('u.userid', $id); 
   $count = $this->db->get()->row(); 
   return (!empty($count) ? $count->token : 0);
}

我希望它会对你有所帮助。

答案 1 :(得分:0)

$query = $this->db
    ->select('u.token')
    ->from('user u')
    ->where('u.userid', $id)
    ->get();

return ($query->num_rows() == 1)    ?   $query->row()->token    :   0;