0

Please help me am not able to get the last inserted Id in medoo

Below is my code:

 <?php
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET,OPTIONS,POST,PUT,DELETE');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        require 'vendor/autoload.php';
        $app = new\Slim\Slim();
        $app->container->singleton('db',function () use ($app) {
            return new medoo([
            'database_type' =>'pgsql',
            'database_name' =>'emergency',
            'server'=>'localhost',
            'username' =>'postgres',
            'password' => 'root',
            'charset' => 'utf8',
            'option' =>[
                PDO::ATTR_CASE=>PDO::CASE_NATURAL
            ]
            ]);
        });
$app->db->post('/getinfo',function()
{
$body = $app->request->post();
 $app->db->insert("emergencymessages", [

            "message" =>$body["msg"],
            "createdby"=>$createdby
            ]); 
});

Now i want to get last inserted id of emergencymessages table how can i get it??

lazyCoder
  • 2,544
  • 3
  • 22
  • 41

1 Answers1

1

According to the docs (http://medoo.in/api/insert) the insert function returns the last insert id.

$last_insert_id = $app->db->insert("emergencymessages", [
              "message" =>$body["msg"],
              "createdby"=>$createdby
            ]); 

EDIT:

What you can try is to call lastInsertId(); directly on medoo's pdo object:

$last_insert_id = $app->db->pdo->lastInsertId();
eol
  • 23,236
  • 5
  • 46
  • 64