0

我又来了,我快疯了!我有这个存储过程:

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

我怎样才能得到最后一个插入ID???我尝试了所有,没有参数或在表上使用 max(id) 进行选择或“设置 last_id = last_insert_id()”,但我不知道如何通过 php 获取它?

当我在 phpmyadmin 中调用该过程时,我得到了类似的东西

@rgt:=number,@lft:=number2

而在 php 中,我显然在数组中得到了相同的结果。

我做错了什么?

4

3 回答 3

1
于 2013-12-11T18:34:17.270 回答
1

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.

于 2013-12-11T21:45:03.740 回答
0

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;');
于 2013-12-11T19:59:53.283 回答