0

I tried but I can't get insert_id from the function. I have made a class call functions but I don't understand how can I get it. See below code.

class DatabaseConnection {
protected $pdo;
public function __construct() {
 try {
   $this->pdo = new PDO('mysql:host=localhost;dbname=xxx', 'root', '');
 } catch(PDOException $e) {
  exit('Database error');
  }
 }
}

class Invoice extends DatabaseConnection {
  public function order($rename, $location) {
  $query = $this->pdo->prepare("INSERT INTO orderlist (re_name, location) VALUES (?, ?)");
  $query -> execute(array($rename, $location));
}

$user = new Invoice();
if($_SERVER['REQUEST_METHOD'] == 'POST') {
   $rename = $_POST['re_name'];
   $location = $_POST['location'];
   $insertOrder  = $user->order($rename, $location); // call the function here
   $insertid = ____ // I want to get id using insert_id in here see blank space.
}

Data is inserted successfully but how can I get id which I insert data recently. help me.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ankit Parmar
  • 670
  • 10
  • 24

1 Answers1

2

Wouldn't it work if you tried :

$user->getPdo->lastInsertId();

Details

urfusion
  • 5,528
  • 5
  • 50
  • 87
Zyigh
  • 425
  • 4
  • 16