I have read the MySQL-bug list, where I found out, that last_insert_id() returns the FIRST auto_increment-value when you're performing multiple insert statements.
(Link: http://bugs.mysql.com/bug.php?id=34319)
My problem is, that in my code, I actually only perform 1 insert query after I've performed some select to get the values for my nested-sets. But the last_insert_id() still returns "0" as ID even though I only have 1 insert query. Do you know what could be the problem? :)
$query = $this->prepare("
SELECT @myRight := c.Rgt FROM ".DB_PREFIX."C_Categories c
WHERE c.CategoryID = :cat_id;
UPDATE ".DB_PREFIX."C_Categories c1 SET c1.Rgt = c1.Rgt + 2 WHERE c1.Rgt >= @myRight;
UPDATE ".DB_PREFIX."C_Categories c2 SET c2.Lft = c2.Lft + 2 WHERE c2.lft > @myRight;
INSERT INTO ".DB_PREFIX."C_Categories
(CategoryName, MetaTag, Description, ParentID, Lft, Rgt)
VALUES (:cat_name, :cat_tag, :desc, :parent_id, @myRight, @myRight+1);
");
$params = array(
array('cat_name', $data['name'], 'STR'),
array('cat_tag', $data['metatag'], 'STR'),
array('desc', $data['description'], 'STR'),
array('parent_id', $data['parentid'], 'INT'),
array('cat_id', $parent_id, 'INT')
);
$this->exec($query, $params);
$id = $this->lastInsertId();
return $id;