用准备好的声明返回id

时间:2013-11-11 23:49:40

标签: php prepared-statement

我已经创建了一个注册页面,其中一切正常。只想让这个小插件返回id(设置为自动增量)。我该怎么做呢?

$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();
}

1 个答案:

答案 0 :(得分:2)

您可以从连接变量中读取它:

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

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