Lachezar Raychev
Lachezar Raychev

Reputation: 2113

Php variable representation

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.

Upvotes: 0

Views: 63

Answers (2)

ulentini
ulentini

Reputation: 2412

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

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

Upvotes: 3

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

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

Upvotes: 2

Related Questions