php - How to access last inserted id in PDO with static variable? -
below code.i want access last inserted id. since use static connection variable , gives me error accesssing in way: $insertedid = $stmt->connection::$pdo->lastinsertid() ;
public function addcar() { $this->rate=$param6; if(!empty($this->name)) { $sql="insert car(car_name,car_maker,car_type,car_colour,num_passanger)values('{$this->name}','{$this->maker}', '{$this->type}','{$this->colour}','{$this->passanger}')"; $stmt =connection::$pdo->prepare($sql); $stmt->execute(); echo "inserted id ".$insertedid = $stmt->lastinsertid() ; } //$this->rentalrate(); }
update:
simple way use lastinsertid()
is:
// connection database $con = new pdo("mysql:host=$hostname;dbname=$dbname",$dbuser,$dbpass); // insert query $query = "insert tbl_name set col_name1 = ?, col_name2 = ?"; // '$con' pdo connection variable $stmt = $con->prepare($query); $stmt->bindparam(1, $variable1); // value col_name1 stored $stmt->bindparam(2, $variable2); // value col_name2 stored $stmt->execute(); .... // gives current inserted id $lastid = $con->lastinsertid();
in case try: $lastid = connection::$pdo->lastinsertid();
Comments
Post a Comment