最後一條記錄使用這個PHP代碼:獲取在MySQL
try{
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// INSERT CLEAN DATA INTO TABLE…
$sth = $dbh->prepare("
INSERT INTO Fan(fanNm,fanEmail,fanPass,fanDynamSalt)
VALUES('$userName','$userEmailAddress','$userPassword','$dynamSalt')"
);
$sth->execute();
////////////////////////////////////////////////////////////////////
## Set Session Var for this PK ID in Fan table that is being created ##
////////////////////////////////////////////////////////////////////
$_SESSION['newUserSessID'] = mysql_insert_id();
echo "<strong style='color:#fff;'>".$_SESSION['newUserSessID']."</strong>";
} //try
catch(PDOException $e){
echo "Oops, We're experiencing an error.";
file_put_contents('/PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
它插入FINE到數據庫中,但我使用echo "<strong style='color:#fff;'>".$_SESSION['newUserSessID']."</strong>";
呼應的是,值,它始終返回零。
即使我運行:
SELECT LAST_INSERT_ID()
FROM Fan
LIMIT 0 , 30
它給我像
0
0
輸出(因爲在數據庫表中兩行)
有人嗎?
mysql_insert_id()檢索先前查詢爲AUTO_INCREMENT列生成的ID。您的任何欄目是否爲AUTO_INCREMENTS?範的其他領域是什麼? – 2012-03-14 01:56:07
HTML標籤需要雙引號('''),而不是單引號('''),你在'echo'語句中有。 – Jon 2012-03-14 01:57:09
@AmitBhargava - 是的PK是自動增量。 – CodeTalk 2012-03-14 01:58:22