Last ID lastInsertId

1

Good afternoon

Personal I'm trying to get the ID that was entered by my class but without success. 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();

   }

**** CLASS CONNECT ******* 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);
}

}

    
asked by anonymous 25.10.2016 / 21:29

2 answers

0

Tried:

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

If yes what error does it present?

    
25.10.2016 / 21:40
0

The last ID you entered is saved to your database instance.

Conecta::getInstance()->lastInsertId();
    
26.10.2016 / 14:51