-1
有誰知道爲什麼我得到的返回值爲0,此代碼:PHP MySQL的lastInsertID
/* check connection */
if (mysqli_connect_errno()) {
error_log("Connect failed: " . mysqli_connect_error());
echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';
} else {
$stmt = $mysqli->prepare("INSERT INTO teams (name, token) VALUES (?, ?)");
$stmt->bind_param('ss', $team, $token);
/* execute prepared statement */
$stmt->execute();
if ($stmt->error) {error_log("Error: " . $stmt->error); }
$success = $stmt->affected_rows;
//Get the last insert ID of the insert Team
$lastInsertID = $stmt->insert_id;
//Insert into the Mapping Table
$stmt = $mysqli->prepare("INSERT INTO usersTeamsMap (users_idUser, teams_idTeam) VALUES (?, ?)");
$stmt->bind_param('ii', $userID , $lastInsertID);
/* execute prepared statement */
$stmt->execute();
//Get the last insert ID of the insert Team
$lastInsertID = $stmt->insert_id;
echo "ID: " . $lastInsertID;
最後的回聲總是返回0。第一個 「$ lastInsertID = $ stmt-> INSERT_ID;」第一次插入查詢的工作正常。
在此先感謝。
編輯:
感謝您的所有答案。現在我知道爲什麼服務沒有工作。我的映射表沒有AutoIncrement字段。
什麼是第一個也是最後一個?是宇跑循環?代碼的那一部分在哪裏 – 2015-03-08 18:47:37
在第一個之後並且在你準備第二個之前嘗試'$ stmt-> close();'。 – 2015-03-08 18:52:13