select_max无法正常工作..它总是导致1

时间:2014-07-03 05:20:36

标签: php codeigniter

这是在我的模型中..它说类CI_DB_mysql_result的对象无法转换为int ..问题出在select_max('id'); 它总是导致1.请帮助我是codeigniter的新手

public function addEstablishment()
{
   $capacity = $this->input->post('capacity');

   $name = $this->input->post('name');
   $curfew = $this->input->post('curfew');
   $price = $this->input->post('price');
   $gender = $this->input->post('gender');
   $type = $this->input->post('type');

   $this->db->select_max('id');
   $result = $this->db->get('establishment');
   $query = $result + 1;


   $owner = $this->session->userdata('id');

   $data = array(
      'id' =>  $query,
      'name' => $name ,
      'capacity' => $capacity ,
      'curfew' => $curfew ,
      'gender' => $gender ,
      'type' => $type , 
      'owner' => $owner
    );

   if($this->db->insert('establishment', $data))
   {
      echo "Successfully added";
   } 
}

1 个答案:

答案 0 :(得分:1)

你的结果没有max ID,请尝试这种方式

变化

$this->db->select_max('id');
$result = $this->db->get('establishment');

$this->db->select_max('id');
$result = $this->db->get('establishment')->row()->id;