SELECT MAX column value relating to the MAX column value of another table with CodeIgniter's query builder
09:03 18 Jan 2012

I'm trying to get a max value with CodeIgniter from an table but it isn't working. This is the error I get:

Severity: 4096

Message: Object of class CI_DB_mysql_result could not be converted to string

Filename: database/DB_active_rec.php

Line Number: 427

This is my function:

public function getPeriodeNummer($bedrijf_id)
{
     $this->db->select_max('id');
     $this->db->where('bedrijf_id', $bedrijf_id);
     $result = $this->db->get('rapporten');
    
     $this->db->select('periode_nummer');
     $this->db->where('rapporten_id', $result);
     $query = $this->db->get('statistieken_onderhoud');
    
     $data = $query + 1;
    
     return $data;
}

What I'm trying to do is as followed:

  1. Select the highest id where bedrijf_id = $bedrijf_id from rapporten.
  2. Select the periode_nummer from statistieken_onderhoud where rapporten_id = the highest id I got from step 1.
  3. Add 1 to the periode_nummer I got from step 2 and return that number.
php mysql codeigniter max query-builder