Choose max codeigniter

I am trying to get the maximum value using codeigniter from a table, but it does not work. This is the error I get:

Weight: 4096

Message: object of class CI_DB_mysql_result cannot be converted to a string

File Name: 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 the following:

  • Select the highest id where bedrijf_id = $bedrijf_id from rapporten .
  • Select periode_nummer from statistieken_onderhoud , where rapporten_id = the highest id obtained from step 1.
  • Add 1 to periode_nummer , I got from step 2 and return is a number.

Thank you for your help!

+5
source share
5 answers

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; } 
+15
source

Try the following:

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

Display Sequence is the name of your column, and acl_menu is the name of your table.

+2
source

You cannot 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; } 
+1
source

I think the $query variable contains the mysql result resource and cannot be used as String or in this case Integer.

You can try this way:

 $data = mysql_result($query,0) + 1; 
0
source
 $this->db->select_max('id', 'max_id'); $query = $this->db->get('video_processing'); return $query->row(); 

try above:

0
source

Source: https://habr.com/ru/post/1391638/


All Articles