Get last insert id from last inserted row

佐手、 提交于 2019-12-11 19:41:36

问题


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??? 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?

when i call the procedure in phpmyadmin i'm getting something like

@rgt:=number,@lft:=number2

and in php obviously i get the same result in array.

what i'm doing wrong?


回答1:


You should be able to use mysql_insert_id‎ to fetch the id for the last insert statement.
Reference: http://php.net/mysql_insert_id‎

However, mysql_* functions are, now, deprecated (read below) and you should use an equivalent mysqli_* function or method. The equivalent for the above function would be: $mysqli->insert_id()
Reference: http://www.php.net/manual/en/mysqli.insert-id.php

The mysql_* category of functions have been deprecated, and the warning is reproduced here:

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

  • mysqli_insert_id()
  • PDO::lastInsertId()



回答2:


If you're using MySQLi then your would have something like this.

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:

$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.




回答3:


Ok i solved it!

i added an int output parameter called last_id, before the end tag i included this statement:

select last_insert_id() into last_id;

In PHP i execute:

$db -> execQuery('call aggiungi_categoria('.$parametri['id_categoria_principale'].',"'.$parametri['nome'].'","'.$parametri['sigla'].'",'.$parametri['stato'].',@lastId);');

and finally

$lid = $db ->execQuery('select @lastId;');


来源:https://stackoverflow.com/questions/20527109/get-last-insert-id-from-last-inserted-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!