0

I'm trying to get the last insert id from a plugin I'm in the process of creating for vBulletin in PHP. For some reason it doesn't seem to be working... Any ideas how I go about it?

  • The query is inserting fine using the $db->query_write($sql_i);
  • The documentation I have found states that I need to use $db->insert_id

Full Code:

$sql_i = "INSERT INTO classifieds_item (".$i_fieldnames.",`date_posted`) values (".$i_values.",NOW())";

$db->query_write($sql_i);

header("location: classifieds.php?class_act=add_img&id=".$db->insert_id);

This is using the standard vBulletin database class.

Any ideas?

2 Answers 2

4

insert_id() is a function, so

header("location: classifieds.php?class_act=add_img&id=".$db->insert_id());

should work. The crucial point is the difference between $db->insert_id and $db->insert_id().

1
  • Thanks - I can't vote you up as I'm new here but that solved it
    – trvo
    Commented Sep 13, 2012 at 12:54
0

Have you tried mysql_insert_id(): http://php.net/manual/en/function.mysql-insert-id.php

It will return the last id for an AUTO_INCREMENT column by the previous INSERT.

Worth noting that the PHP documentation also suggests using alternatives as this method is now discouraged.

1
  • Thanks for you reply, but because I am building a portable plugin I need to stick to using vBulletin classes where possible. For the native PHP functions to work I need a resource identifier which I do not have and would have to rewrite the plugin to cater for this. This is at the client's request.
    – trvo
    Commented Aug 8, 2012 at 20:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.