Klevis Cipi Klevis Cipi - 2 months ago 8
MySQL Question

PDO return lastInsertId() zero 0, i dont understand?

this is my code ?
pdo return always lastid 0, i tray everething.!

$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();
Comments