I need to generate a link to the current game, I didn’t think of anything better how to use lastInsertId , I ’m writing to OOP, and it turns out that the result of executing the code eventually displays 0 instead of the last id
class CheckStart extends Db { public $data; public function __construct() { $this->data = $_POST; } public function checkStart() { if ( isset($this->data['do_start']) ) { $hp_default = 1000; // Стандартное количество хп $sql = $this->connect()->prepare("INSERT INTO `game` (`hp`) VALUES (:hp)"); $sql->bindParam(":hp", $hp_default, PDO::PARAM_INT); $sql->execute(); echo "success"; // header("Location: index.php"); } else { echo "fail"; } } public function generateLink() { // Генерирования ссылки на игру $sql = $this->connect()->query("SELECT `id` FROM `game`"); $sql = $sql->fetch(PDO::FETCH_ASSOC); $sql = $this->connect()->lastInsertId(); echo $sql; } } $start = new CheckStart; $start->checkStart(); $start->generateLink();`
INSERT->SELECT->lastInsertId(). It is not right.lastInsertId()must be afterINSERTa. Why do you need thisSELECT? try deleting the first 2 lines fromlastInsertId()- Manitikylpublic function generateLink() { // Генерирования ссылки на игру $sql = $this->connect()->lastInsertId(); echo $sql; }public function generateLink() { // Генерирования ссылки на игру $sql = $this->connect()->lastInsertId(); echo $sql; }public function generateLink() { // Генерирования ссылки на игру $sql = $this->connect()->lastInsertId(); echo $sql; }- Ulquiorra SifferINSERT, the answer immediately comes and is written tolastInsertId. And with yourSELECTyou are likely to reset it. - Manitikylecho "success";onecho $this->connect()->lastInsertId();. If it is0again, then we will think. - Manitikyl0- Ulkiorra Sifer