Last lastInsertId ID

Asked

Viewed 386 times

1

Good afternoon

Guys I’m trying to get the ID that was entered by my class but unsuccessfully. The examples I found none worked. If anyone can help, thank you.

public function insert_orcamento($numero, $cod_cliente) {


    $sql = "INSERT INTO $this->table (numero, cod_cliente)"
            . "VALUES (:numero, :cod_cliente )";

            $stmt = Conecta::prepare($sql);

    $stmt->bindParam(':numero', $numero);
   $stmt->bindParam(':cod_cliente', $cod_cliente);


   return $stmt->execute();

   }

****CONNECT CLASS******* require_once dirname(DIR).DIRECTORY_SEPARATOR.'config'. DIRECTORY_SEPARATOR.'config.php';

class Connects {

private static $instance;
public static function getInstance() {
    if (!isset(self::$instance)) {
        try {
            self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname='.DB_NAME, DB_USER, DB_PASS);
            self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    return self::$instance;
}
public static function prepare($sql) {
    return self::getInstance()->prepare($sql);
}

}

2 answers

0

The last ID inserted is saved in your database instance.

Conecta::getInstance()->lastInsertId();

0

Tried to :

$stmt->execute();
$id = $db->lastInsertId();

If yes which error shows ?

  • Placing the code above, gives the error below. Fatal error: Call to a Member Function lastInsertId() on a non-object in C: wamp www incoterm classes Incluorcamento.php on line 226

Browser other questions tagged

You are not signed in. Login or sign up in order to post.