我一直在尋找,在其他問題相同,並不能找出爲什麼我的查詢不會像它應該。PDO lastInsertId()返回0
我的查詢:
$stmt = db()->prepare("INSERT INTO conversations (user1, user2) VALUES (?, ?)");
$stmt->execute(array($_SESSION['user']['userId'], $user));
echo db()->lastInsertId();
當我做到這一點,lastInsertId();保持返回0。
我的DB()函數:
function db()
{
$dsn = 'mysql:host=localhost;dbname=message_board';
$username = 'root';
$password = 'root';
try {
$db = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
// exceptions handles here
}
return $db;
}
您正在每次調用一次新的'PDO'對象'分貝()'。只要檢查它是否已經存在。 –