2012-10-03 40 views
0
if(!class_exists('MySql')) 
{ include('MySql.php'); } 
    $sql=new MySql(); 
    $sql->connect(); 
    $sqlCommand="insert into `freecomputermarket`.`members` 
(`UserName`,`Password`,`Email`,`BirthDate`,`RegisterationDate`,`ActivationCode`, 
`ActivationLink`,`IsActive`,`Gender`) 
values('$this->_userName','$this->_password','$this->_email','$this->_birthDate', 
'$this->_registerationDate','$this->_activationCode','$this->_activationLink', 
'$this->_isActive','$this->_gender')"; 
    $sql->query($sqlCommand); 

我怎麼能得到插入的自動增量ID?從MySQL自動遞增值插入查詢

回答

2

你已經很好地隱藏了你的MySql類的實現細節。

  • 如果您正在使用PDO,你會想看看LastInsertId()
  • 如果您正在使用mysqli的,看mysql_insert_id();
  • 如果你正在使用MySQL的:切換到PDO或mysqli的
+1

狡猾的第三點! – nickhar

0
$sql->query($sqlCommand); 
$id = lastInsertId(); 

文檔瀏覽:http://php.net/manual/en/pdo.lastinsertid.php

+0

這個函數是否會在例如命令被選中時引發錯誤? 或返回null? –

+0

您將不得不嘗試/捕捉任何異常,請參閱我之前鏈接中的示例(在評論中)。 – martinwnet