【问题标题】:active record codeigniter活动记录代码点火器
【发布时间】:2017-11-21 15:11:04
【问题描述】:

如何在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;

【问题讨论】:

  • 你只需要做这个$this->db->select('u.token')->from('user u')->where('u.userid', $id); $count = $this->db->get()->row(); return ( count($count) > 1) ? $count->token : 0;
  • 感谢您的帮助:)

标签: php sql codeigniter activerecord


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

希望对你有帮助。

【讨论】:

    【解决方案2】:
    $query = $this->db
        ->select('u.token')
        ->from('user u')
        ->where('u.userid', $id)
        ->get();
    
    return ($query->num_rows() == 1)    ?   $query->row()->token    :   0;
    

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多