-2
之後獲取PDO lastInsertId()在我的代碼中,我使用methods
將數據插入到MySQL
數據庫中。該方法然後返回true
或false
。在調用方法
如果method
返回true
我想使用lastInsertId()
來運行第二種方法。
像
if($db->insertMethod($data)){
$lastId = $db->lastInsertId();
$db->secondInsertMethod($lastId, $data2)
}
db.class.php
public function insertMethod($data) {
$db = new DB();
$insert = //Run SQL insert
if($insert > 0) {
return true;
}else{
return false;
}
}
public function lastInsertId() {
return $this->pdo->lastInsertId();
}
在這種情況下$db->lastInsertId();
將返回0
。一種解決方法是讓insertMethod
返回此代替true
,但還有其他方法嗎?
當使用'return $ this-> lastInsertId();'方法調用時,它非常有意義。所以這個價值正是我想要的,但是它的方式讓它超越了我所追求的方法。 – Andy