我有以下代码用于使用 PDO 将数据插入数据库。
它将数据插入数据库,但不返回最后插入的 ID。
这userid
是主键
try {
$dbh = new PDO('mysql:host=localhost;dbname=crud_demo', "username", "password");
$sqlQuery = "INSERT INTO users(userid,first_name,last_name,email,password)
VALUES(:userid,:first_name,:last_name,:email,:password)";
$statement = $dbh->prepare($sqlQuery);
$bind = array(
":userid" => "bhavik",
":first_name" => "Bhavik",
":last_name" => "Patel",
":email" => "bhavitk@live.in",
":password" => "1234567"
);
$statement->execute($bind);
echo $dbh->lastInsertId();
} catch (PDOException $e) {
echo $e->getMessage();
}
$dbh->lastInsertId();
无论我为用户 ID 插入什么值,总是返回 0