[英]Get last insert id from last inserted row
I'm here again and I'm getting crazy! 我又来了,我疯了! I have this stored procedure: 我有这个存储过程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `aggiungi_categoria`(IN `id_categoria_principale` INT, IN `nome_categoria` VARCHAR(150), IN `sigla_categoria` VARCHAR(10), IN `stato_categoria` INT) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
begin
select @rgt := rgt,@lft := lft from categorie where id = id_categoria_principale;
if @rgt - @lft = 1 then
UPDATE categorie SET rgt = rgt + 2 WHERE rgt > @lft;
UPDATE categorie SET lft = lft + 2 WHERE lft > @lft;
insert into categorie (nome,sigla,stato,lft,rgt)
values(nome_categoria,sigla_categoria,stato_categoria,@lft+1,@lft+2);
ELSE
UPDATE categorie SET rgt = rgt + 2 WHERE rgt > @rgt-1;
UPDATE categorie SET lft = lft + 2 WHERE lft > @rgt-1;
insert into categorie (nome,sigla,stato,lft,rgt) values
(nome_categoria,sigla_categoria,stato_categoria,@rgt,@rgt+1);
end if;
end
how can i get the last insert id??? 我怎么能得到最后一个插入ID? i tried all, with out param or doing a select with max(id) on the table or "set last_id = last_insert_id()" but i don't know how to get it via php? 我尝试了所有,使用param或在表上执行选择max(id)或“set last_id = last_insert_id()”但我不知道如何通过php获取它?
when i call the procedure in phpmyadmin i'm getting something like 当我在phpmyadmin中调用该程序时,我得到了类似的东西
@rgt:=number,@lft:=number2
and in php obviously i get the same result in array. 在PHP中显然我在数组中获得相同的结果。
what i'm doing wrong? 我做错了什么?
You should be able to use mysql_insert_id
to fetch the id for the last insert statement. 你应该能够使用mysql_insert_id
来获取ID为最后的INSERT语句。
Reference: http://php.net/mysql_insert_id 参考: http : //php.net/mysql_insert_id
However, mysql_*
functions are, now, deprecated (read below) and you should use an equivalent mysqli_*
function or method. 但是,现在不推荐使用mysql_*
函数(如下所示),您应该使用等效的mysqli_*
函数或方法。 The equivalent for the above function would be: $mysqli->insert_id()
上述函数的等价物是: $mysqli->insert_id()
Reference: http://www.php.net/manual/en/mysqli.insert-id.php 参考: http : //www.php.net/manual/en/mysqli.insert-id.php
The mysql_*
category of functions have been deprecated, and the warning is reproduced here: 不推荐使用mysql_*
类别的函数,并在此处复制警告:
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. 自PHP 5.5.0起,此扩展已弃用,将来将被删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应该使用MySQLi或PDO_MySQL扩展。 See also MySQL: choosing an API guide and related FAQ for more information. 另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。 Alternatives to this function include: 该功能的替代方案包括:
If you're using MySQLi then your would have something like this. 如果你使用的是MySQLi,那么你会有类似的东西。
Procedural: 程序:
$link = mysqli_connect($db_host, $db_user, $db_pass, $db_table);
$query = "INSERT INTO table VALUES($val, $val2, $val3)";
mysqli_query($link, $query);
$id = mysqli_insert_id($link);
OOP: OOP:
$mysqli = new mysql($db_host, $db_user, $db_pass, $db_table);
//query
$id = $mysqli->insert_id;
Hope this helps! 希望这可以帮助!
Let me know if you have any issues. 如果您有任何问题,请告诉我。
Ok i solved it! 好的,我解决了!
i added an int output parameter called last_id, before the end tag i included this statement: 我添加了一个名为last_id的int输出参数,在结束标记之前我包含了这个语句:
select last_insert_id() into last_id;
In PHP i execute: 在PHP中我执行:
$db -> execQuery('call aggiungi_categoria('.$parametri['id_categoria_principale'].',"'.$parametri['nome'].'","'.$parametri['sigla'].'",'.$parametri['stato'].',@lastId);');
and finally 最后
$lid = $db ->execQuery('select @lastId;');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.