Home > Backend Development > PHP Tutorial > PHP 使用 POD 访问mysql数据库,如何返回上一个插入记录的自增编号ID

PHP 使用 POD 访问mysql数据库,如何返回上一个插入记录的自增编号ID

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 14:38:52
Original
1300 people have browsed it

PHP 使用 POD 访问mysql数据库,如何返回上一个插入记录的自增编号ID


回复讨论(解决方案)

string PDO::lastInsertId ([ string $name = NULL ] )

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() . ""; 
    } 
} catch( PDOExecption $e ) { 
    print "Error!: " . $e->getMessage() . ""; 

?> 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template