Klevis Cipi Klevis Cipi - 2 months ago 7
MySQL Question

PDO return lastInsertId() as Zero (0), i didn't understand?

This is my code ? PDO return always the last inserted

id
as 0, I tried everything.!

$this->db()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO inbox (sender,recever) VALUES ('1','2')";
$this->db()->exec($sql);
$project_id =$this->db()->lastInsertId();


The
sql
code table :

CREATE TABLE inbox(
id int(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sender int,
recever int
);

Answer

Try this

//set persistent connection
$con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->use‌​r,$this->pass,array(‌​PDO::ATTR_PERSISTENT => TRUE));    
$stmt = $con->prepare("INSERT INTO inbox (sender,recever) VALUES ('1','2')");
$stmt->execute();
$id = $db->lastInsertId();