如何使用Phalcon获取最后的插入ID? [英] How to get last insert id with Phalcon?
本文介绍了如何使用Phalcon获取最后的插入ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在获取自动增量主键列的最后一个id时尝试使用LAST_INSERT_ID()
,但出现了EOF异常:
I tried using LAST_INSERT_ID()
when getting the last id of the autoincrement primary key column but I get EOF exception :
function add($tab) {
$champs= "";
$value = "";
$separateur ="";
$tab["commande_date"] = convertDateFormat5($tab["commande_date"]);
foreach ($tab as $k => $v){
if ($k == "salle_code" || $k == "table_code")
continue;
$champs .= $separateur . $k;
$value .= $separateur . "'" . $v . "'";
$separateur = ",";
}
$champs = '('.$champs.')';
$value = '('.$value.')';
$sSQL = "
INSERT INTO Commande $champs
VALUES $value
";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$sSQL = "SELECT LAST_INSERT_ID() as last_id";
$queryId = new Query($sSQL,$this->getDI());
return $queryId->execute();
}
那么如何使用Phalcon
获取最后一个ID?
So how to get the last id with Phalcon
?
推荐答案
您可以使用lastInsertId()
函数
$sSQL = "INSERT INTO Commande $champs VALUES $value ";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$lastId = $query->lastInsertId();
更新
这里是插入并获取lastinsert ID的示例.
Here is an example of inserting and getting back the lastinsert id.
$success = $connection->insert(
"robots",
array("Astro Boy", 1952),
array("name", "year")
);
//Getting the generated id
$id = $connection->lastInsertId();
我认为这可能对您有帮助.
I think this might help you.
这篇关于如何使用Phalcon获取最后的插入ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文