6

Im trying to get an max value with codeigniter from an table but it isnt 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 im 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.

Thanks in forward for your help!

gustyaquino
  • 767
  • 1
  • 7
  • 28
Augus
  • 495
  • 1
  • 8
  • 24

6 Answers6

18

Try

public function getPeriodeNummer($bedrijf_id) {
    $this->db->select_max('id');
    $this->db->where('bedrijf_id', $bedrijf_id);
    $res1 = $this->db->get('rapporten');

    if ($res1->num_rows() > 0)
    {
        $res2 = $res1->result_array();
        $result = $res2[0]['id'];

        $this->db->select('periode_nummer');
        $this->db->where('rapporten_id', $result);
        $query = $this->db->get('statistieken_onderhoud');

        if ($query->num_rows() > 0)
        {
            $row = $query->result_array();
            $data['query'] = 1 + $row[0]['periode_nummer'];
        }

        return $data['query'];
    }

    return NULL;
}
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
  • 1
    And this works perfectly, now the challenge to see what its doing to learn from it. But it works, thanks alot!. – Augus Jan 18 '12 at 14:25
  • @Angus -- Please see CodeIgniter's user guide entries for [example queries](http://codeigniter.com/user_guide/database/examples.html) and [Active Record](http://codeigniter.com/user_guide/database/active_record.html) for further explanation and reference for the functions I used. – stealthyninja Jan 18 '12 at 14:39
2

Try this:

    $this->db->select_max('display_sequence');
    $this->db->from('acl_menu');
    $query = $this->db->get();
    $r=$query->result();

Display Sequence is your column name & acl_menu is your table name.

1
$this->db->select_max('id', 'max_id');
$query = $this->db->get('video_processing');

return $query->row();

try the above:

Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
1

You can't use an object as a string. Use this:

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');
    // fetch first row in object
    $result = $query->row();
    $data = $result + 1;

    return $data;
}
mirza
  • 5,685
  • 10
  • 43
  • 73
0

Shortest:

$this->db->select_max('id', 'max_id')->get('video_processing')->row();
pgrono
  • 740
  • 7
  • 5
0

I think the $query variable is holding a mysql result resource and it cannot be used as a String or in this case an Integer.

You could try this way:

$data = mysql_result($query,0) + 1;
gustyaquino
  • 767
  • 1
  • 7
  • 28
  • Tried it, but didnt work, I also tried to make `$result` and mysql_result but that gave me this error `Message: mysql_result() expects parameter 1 to be resource, object given` – Augus Jan 18 '12 at 13:28