문제

I want to add a user to the database. Every user has an unique ID that is automatic created(A_I). If I add an user I want to get this ID. I'm trying to do this with "insert_id", but if I do this I get an error.

$sth=Connection()->prepare ("INSERT INTO Users (Username, Password) VALUES(:Username, :Password)");
$sth->BindValue(":Username", $username, PDO::PARAM_STR);
$sth->BindValue(":Password", $password, PDO::PARAM_STR);
$sth->execute();
$ID = $sth->insert_id;

Can anyone tell me what I'm doing wrong?

도움이 되었습니까?

해결책

You are using PDO not mysqli. You need to use PDO::lastInsertId(). Like this:

$con = Connection();
$sth= $con->prepare ("INSERT INTO Users (Username, Password) VALUES(:Username, :Password)");
$sth->BindValue(":Username", $username, PDO::PARAM_STR);
$sth->BindValue(":Password", $password, PDO::PARAM_STR);
$sth->execute();
$ID = $con->lastInsertId();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top