Difficulties with PHP - lastInsertId()

Asked

Viewed 1,029 times

0

I’m having trouble getting the last id inserted in my table using the php PDO function lastInsertId() How do I get the last id inserted in my table using my structure?

Connection to the bank

class ConnectionDB{
 private function setConnection(){
   try {
     $con = new PDO("mysql:host=localhost;dbname=easyjobapi", "root", "");
     $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $con->exec("SET NAMES 'utf8'");
     return $con;
   } catch (PDOException $e) {
     echo "Erro ao conectar-se: ".$e->getMessage();
   }
 }
 public function getConnection(){
   return $this->setConnection();
 }
}

Test file

$con = new ConnectionDB();
/*tabela teste so contem um coluna chamada id AUTO_INCREMENT e a coluna nome*/
$stmt2 = $con->getConnection()->prepare("INSERT INTO teste (nome) VALUES ('MAX123')");
$stmt2->execute();
var_dump($con->lastInsertId());

Error presented

<br />
<b>Fatal error</b>:  Uncaught Error: Call to undefined method ConnectionDB::lastInsertId() in C:\xampp\htdocs\easyjob\api\site\teste.php:10
Stack trace:
#0 {main}
  thrown in <b>C:\xampp\htdocs\easyjob\api\site\teste.php</b> on line <b>10</b><br />

I’ve tried too:

$lastId = $con->lastInsertId();
var_dump($lastId);

But you still give me the error:

<br />
<b>Fatal error</b>:  Uncaught Error: Call to undefined method ConnectionDB::lastInsertId() in C:\xampp\htdocs\easyjob\api\site\teste.php:10
Stack trace:
#0 {main}
  thrown in <b>C:\xampp\htdocs\easyjob\api\site\teste.php</b> on line <b>10</b><br />

How to proceed?

  • The example of this answer is using a Postgres function...

1 answer

0


face I think your class of connection may be wrong try that

connection

<?php
class ConnectionDB{
private $conn;  
public function __construct(){
try {
     $con = new PDO("mysql:host=localhost;dbname=easyjobapi", "root", "");
     $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $con->exec("SET NAMES 'utf8'");
     $this->conn = $con;

   } catch (PDOException $e) {
     echo "Erro ao conectar-se: ".$e->getMessage();
   }
 }
 public function getConnection(){
 return $this->conn;
 }

}

test

  $con = new ConnectionDB();
  /*tabela teste so contem um coluna chamada id AUTO_INCREMENT e a coluna nome*/
  $stmt2 = $con->getConnection();
  $stmt2->prepare("INSERT INTO teste (nome) VALUES ('MAX123')");
  $stmt2->execute();
  $las_id = $stmt2->lastInsertId();
  $id = var_dump($las_id);
  • Vlw by the answer, I thought to do so, but is that I would like to use the lastInsertId(), because in my project I do more than 1 Insert and using this method proposed by Voce, would not be viable, I insert data in a table and then with the id generated from this table I insert data in another table.

  • @Maxrogério your question already has the answer here

  • @Maxrogério I think instead var_dump($con->lastInsertId()); must be $lastId = $con->lastInsertId() then a var_dump in $lastId

  • @Souza Sorry but it is not my solution, in the answer is apresetanda function returning id, which in the case of Mysql would be SELECT LAST_INSERT_ID();, I believe

  • @Adrianoback, vlw Adriano, but the mistake persists... If it is possible to test my manure, and help me how to solve... I will be grateful.

  • @Maxrogério I will edit my answer

  • @Maxrogério see your class I think you have some problem there

  • 1

    @Adrianoback, I was able to solve based on your answer. Note: I did not need to change my class. I kept the return of the connection in a variable part. As you put it in the example

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.