我想要得到最後一個查詢插入id.but我的代碼總是返回零。Mysqli最後一個插入ID不工作
我的代碼
private function Cnn()
{
return mysqli_connect('localhost','root','root','db');
}
protected function MyCommandInsertId($sql)
{
if(mysqli_query($this->Cnn(),$sql))
{
return mysqli_insert_id($this->Cnn());
}
$this->err = mysqli_error($this->Cnn());
return false;
}
public function Insert()
{
$sql = "insert general_info(name,email,password)
values('".$this->ms($this->name)."','".$this->ms($this->email)."','".md5($this->password)."')";
//print''.$sql.'';
$last_insert_id=$this->MyCommandInsertId($sql);
}
回到這裏mysqli_insert_id($this->Cnn());
總是返回零
mysqli_insert_id()函數返回查詢在具有AUTO_INCREMENT屬性的列的表上生成的ID。如果最後一個查詢不是INSERT或UPDATE語句,或者如果修改後的表沒有包含AUTO_INCREMENT屬性的列,則此函數將返回零。 [PHP手冊](http://php.net/manual/en/mysqli.insert-id。php) – silverfox
列有AUTO_INCREMENT屬性@silverfox – Twer1971
檢查是否有插入表中的新記錄? – user3422161