user1572796 user1572796 - 6 months ago 20
PHP Question

get the last auto incremented id with medoo

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);

?>

Answer

The insert() method returns the last insert ID.

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

echo $insertId;
Comments