PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] How to get Select max value in codeigniter

 March 05, 2022     codeigniter     No comments   

Issue

Controller:

$next_id = $this->o->next_id();
$data['next_id']=$next_id;

Model:

public function next_id(){
    $this->db->select_max('p_ori_id');
    $max = $this->db->get('orientation_master');
    if($max==0){
        $next_id = 1;
    }else{
        $next_id = 1+$max;
    }
    return $next_id;
}

Return Error:

Object of class CI_DB_mysqli_result could not be converted to int

Please solve problem..


Solution

No offense to @pradeep but you may have some unexpected results if you don't have any rows. I suggest:

public function next_id()
{
   $this->db->select_max('p_ori_id', 'max');
   $query = $this->db->get('orientation_master');
   if ($query->num_rows() == 0) {
      return 1;
   }
   $max = $query->row()->max;
   return $max == 0 ? 1 : $max + 1;
}


Answered By - Alex
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing