简体   繁体   中英

Returning id with Prepared Statement

I've created a registration page where everything works. Just wanna make this little insert return the id (which is set as auto increment). How would i go about doing so?

$sql = "INSERT INTO accounts (street1, suburb, city, postcode, username) VALUES(?, ?, ?, ?, ?)";

if (! $accres = $connection->prepare($sql)){
    $userinfo = "<font color = red>Account preparation failed: (" . $connection->errno . ") " . $connection->error . "</font>";
    showRegisterForm ( $userinfo );
} else {
    $accres->bind_param("sssss", $address, $suburb, $city, $postcode, $username);
    $accres->execute();
}

You can read it from connection variable:

//execute query then:
$id = $connection->insert_id;

http://www.php.net/manual/en/mysqli.insert-id.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM