1

I have this query and I want to put it on my code igniter php.

SELECT MAX(IDnumber) FROM student WHERE IDnumber LIKE '2013256%'

I trued this one but it returns error:

$this->db->select_max('IDnumber');
$this->db->get('student');
$IDnumber = $this->db->like('IDnumber', '2013256', 'after'); 
return $IDnumber;

does anyone have an idea about my case? thanks in advance...

1
  • is this a typo $this->db-select_max ? should't it be $this->db->select_max ? Commented Apr 8, 2014 at 13:47

1 Answer 1

5

You are getting result after $this->db->get(), so use

$this->db->select_max('IDnumber');
$this->db->like('IDnumber', '2013256', 'after'); 
$this->db->from('student');
return $this->db->get();

Also You can use chaining method:

return $this->db->select_max('IDnumber')->like('IDnumber', '2013256', 'after')->get('student');
3
  • I'm pretty sure that's intended to be used as a "fluent interface" as well, so you could drop the repeated $this->db and chain everything together: $this->db->select_max()->from()->where() etc
    – IMSoP
    Commented Apr 8, 2014 at 14:19
  • @IMSoP edited my answer before Your comment ;) Anyway sometimes it more useful to split chains into lines (for studying someone, for example) Commented Apr 8, 2014 at 14:22
  • True. I was actually surprised it was $this->db being chained, and not a separate object representing the particular query. Not how I'd have built it, but then little of CodeIgniter is ;)
    – IMSoP
    Commented Apr 8, 2014 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.