0

I have this code

if($pdo instanceof PDO){
    $this->last = 'lastInsertId'; 
}
else{
     $this->last = 'insert_id'; 
}

When i do the insert query in the datebase, how can i get the last inserted ID. I tried like this

$this->db->{$this->last}; 

But it did not work.

Thanks for the help in advance.

2
  • You need to provide full code, it is not clear which class does insert operation and which tries to get the id. If $this->db is $pdo, then $this->db->last should be used, otherwise, $this->last only Commented Apr 11, 2013 at 13:45
  • no i assign instance of another DB class which uses PDO but returns instance of itself. Commented Apr 11, 2013 at 13:46

2 Answers 2

3

lastInsertId is a method, therefore you should call it as a method, not as a property/field.

$this->db->{$this->last}(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yep i saw that 10 minutes after i posted the question. Thanks alot.
2

Try call_user_func(Array($this->db,$this->last))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.