简体   繁体   中英

Can't get PHP lastInsertId() to work with this setup

I've been using the below as the way I connect to a MySQL database and how I query it for awhile now. It seems to be a little bit of a different set up from a lot of the examples I see online. I can't seem to figure out how to get lastInsertId() to work with this setup:

define("DB_HOST", "localhost");
define("DB_NAME", "mydatabase");

$link = new DBLink("username", "password");

class DBLink {

  private $host = DB_HOST;
  private $dbname = DB_NAME;
  private $user;
  private $pass;

  private $dbConnection;
  private $error;

  private $sql;

  public function __construct($user, $pass) {

  $this->user = $user;
  $this->pass = $pass;

  $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  $options = array(
      PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
      PDO::ATTR_EMULATE_PREPARES => false,
      PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION
    );

   try {
      $this->dbConnection = new PDO($dsn, $this->user, $this->pass, $options);
    }

    catch(PDOException $e){
      $this->error = $e->getMessage();
      echo $this->error;
    }
  }

public function query($query) {
  $this->sql = $this->dbConnection->prepare($query);
}

public function bind($param, $value) {
  $this->sql->bindParam($param, $value);
}

public function execute() {
  return $this->sql->execute();
}

Then I'll add a new row to the database like this:

 $link->query("INSERT INTO users (username, phone, email)
 VALUES (:username, :phone, :email)");

 $link->bind(':username', $username);
 $link->bind(':phone', $phone);
 $link->bind(':email', $email);
 $link->execute();

After adding a row, I want to get the id of the row that was inserted into the database. I've tried:

$link->lastInsertId();

$link->$sql->lastInsertId();

Writing a separate function with it then having the function inside the DBLink class:

$link->getLastID();

public function getLastID() {
   return $this->sql->lastInsertId();
}

And other wild guesses, such as

 $this->dbConnection->lastInsertId();

I also tried all the above variations with insert_id instead of lastInsertId() because I saw that recommended in a few places.

And a few other things, I've wasted about 3 hours on it. Nothing seems to work to return the last inserted id. I've looked at a lot of examples online, but they all do the database query a little differently from how I have it. I believe lastInsertId() is supposed to be on the PDO object, I'm just not sure how to call that the way I have my queries and database link set up. Can anyone help?

Edit: Could it be I don't have the MySQL database set up properly? The primary key for that table is id. Is that how lastInsertId() knows which column to get the "id" from, vs any other column?

I think I used something like this. In the execute() use

public function execute() {
  $this->lastInsertID = null;
  $ret = $this->sql->execute();
  $this->lastInsertID = $this->dbConnection->lastInsertId();
  return $ret;
}

define a variable in the class

private $lastInsertID;

and then

public function getLastID() {
   return $this->lastInsertID;
}

this ensures that the last insert ID is retrieved just after the SQL is executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM