Recently the following error was appearing on my site ‘max_user_connections’, after many searches I found that the problem was because of persistent database connection so I removed it from my code and it worked. Another problem has come up now, my lastInsertId() is returning 0 when I use persistent connection it works if I take it out it fails. I don’t even know what to do anymore. I’ll leave the most important parts of my code here:
Connection:
public function __construct()
{
$dsn = 'mysql:host='.$this->host.';dbname='.$this->bank;
try {
$this->dbh = new PDO($dsn,$this->user,$this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
public function lastId Inserted(){
return $this->dbh->lastInsertId();
}
Searching for the last Id:
if($this->saleModel->store($data)):
$sale_id = $this->db->lastId Inserted();
$data['sale_id'] = $sale_id;
$this->sellModel->sellProduct($data);
$total = $stock->quant_product - $data['quant_sale'];
$this->ProductModel->decreaseSales($data['product_id'],$total);
Url::redirect('sales/sellProducts/'.$sale_id.'');
endif;