Hello,
I was trying out this function and all I get back is zero.

My test table is defined as :
id mediumnint (7) unsigned autoincrement
field1 varchar 50
field2 char 1

$sql = "INSERT INTO table(field1,field2) VALUES ('$field1','$field2')";   
$id = mysql_insert_id();

The record is written and the id value is fine in the MySQL Db table

Am I missing something ?

Thanks

    Try echoing the value of $last_id on the line after the call to mysql_insert_id. My guess is that it was set correctly, but something happened to that variable afterward (went out of scope, misspelled, etc).

      8 days later

      This is driving me nuts. I did as tomhath suggested and it is ZERO immediately after the successful insert into the table.

      Somebody please help or suggest some other way (not max(id) please).

      Cheers

        if this is your code, I doubt it can work at all. you are assigning some text (the sql statement) to a variable, but you don't execute it before calling mysql_insert_id. (I guess your are doing mysql_query sometimes later, else I wouldn't know how your insert statement could be successful 😉)

        it should look something like this:

        $sql = "INSERT INTO table(field1,field2) VALUES ('$field1','$field2')";   
        mysql_query($sql); $id = mysql_insert_id();
          Write a Reply...