m.T
m.T

Reputation: 51

Getting the id of last inserted record

I'm trying to get the id of last inserted record in the Db. But i'm getting the error

      Parse error: syntax error, unexpected T_RETURN in Z:\www\CI4\application\models           \report_model.php on line 69

my model:

       function getLastInserted() {
$query ="SELECT $id as maxID from info where $id = LAST_INSERT_ID()"

return $query; //line 69
       }

my controller:

            function index()
    {           

    $id=$this->report_model->getLastInserted();
    $this->load->view('u_type1',$id);
    }

Upvotes: 1

Views: 39988

Answers (3)

Sadaf Ayaz
Sadaf Ayaz

Reputation: 81

if you want to get last id without insert function

$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');

Upvotes: 1

birderic
birderic

Reputation: 3765

Assuming you are using the CI database library, you can use $this->db->insert_id().

function getLastInserted() {
    return $this->db->insert_id();
}

Upvotes: 26

becomingwisest
becomingwisest

Reputation: 214

I think you are missing a ; on line 67 after the last ".

Upvotes: 9

Related Questions