Search
 
SCRIPT & CODE EXAMPLE
 

PHP

pdo last id


Beware of lastInsertId() when working with transactions in mysql. The following code returns 0 instead of the insert id.



<?php

try {

    $dbh = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');



    $stmt = $dbh->prepare("INSERT INTO test (name, email) VALUES(?,?)");



    try {

        $dbh->beginTransaction();

        $tmt->execute( array('user', 'user@example.com'));

        $dbh->commit();

        print $dbh->lastInsertId();

    } catch(PDOExecption $e) {

        $dbh->rollback();

        print "Error!: " . $e->getMessage() . "</br>";

    }

} catch( PDOExecption $e ) {

    print "Error!: " . $e->getMessage() . "</br>";

}

?>



When no exception is thrown, lastInsertId returns 0. However, if lastInsertId is called before calling commit, the right id is returned.
Comment

get last insert id pdo

$(document).ready(function(){
$.ajax({ url: "database/update.html",
        context: document.body,
        success: function(){
           alert("done");
        }});
});
Comment

PREVIOUS NEXT
Code Example
Php :: getclientoriginalextension laravel 
Php :: SELECT query with PDO 
Php :: display image in laravel 
Php :: custom post type 
Php :: php superglobal 
Php :: php find text in variable 
Php :: php remove prefix from string 
Php :: response()-make laravel pdf 
Php :: disable quantity field in woocommerce 
Php :: Session store not set on request. 
Php :: add script tag to wordpress Head 
Php :: phpmailer send attachment 
Php :: saveAll get all id save cakephp 
Php :: php sqrt 
Php :: php file for image load 
Php :: laravel add item to array 
Php :: remove all items of an array except the last one in php 
Php :: how do decode base64 php 
Php :: get all artisan commands 
Php :: how set variable public in static method in laravel 
Php :: php read csv file into array 
Php :: php artisan storage link cpanel 
Php :: php exercises 
Php :: php remove last 3 letters from string 
Php :: php date start of day 
Php :: laravel weekly data 
Php :: laravel artisan cache clear 
Php :: compile custom/plain css with mix in laravel 
Php :: deactivate auto update wordpress plugins 
Php :: yyyymmdd to yyyy-mm-dd php 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =