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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -