Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the last auto incremented id with medoo

Tags:

php

I am posting a new row to the database but wanted to return the new auto incremented id in the response using medoo. This seems to be generating a new blank row and returning that id.

<?php

   // Independent configuration
  require 'medoo.php';
  $database = new medoo(array( 
  // required 
  'database_type' => 'mysql', 
  'database_name' => 'db', 
  'server' => 'server', 
  'username' => 'user', 
  'password' => 'pw' 

));

   $database->insert("properties", array(
"name" => $_POST['name'],
  "address" => $_POST['address'],
 "address2" => $_POST['address2'],
  "city" => $_POST['city'],
 "state" => $_POST['state'],
  "zip" => $_POST['zip'],
 "lat" => $_POST['lat'],
 "lng" => $_POST['lng'],
 "website" => $_POST['website']
));

$last_id = $database->insert("properties", array(
    "propertyId" => "propertyId"
));

$propertyId = array(propertyId => $last_id);

echo json_encode($propertyId);

?>
like image 939
user1572796 Avatar asked Dec 06 '22 05:12

user1572796


1 Answers

The insert() method returns the last insert ID.

$insertId = $database->insert("properties", array(
  // etc...
));

echo $insertId;
like image 152
SamT Avatar answered Dec 29 '22 01:12

SamT