Hi - I found the mysql_insert_id function to retrieve the last auto generated ID.
Should I be using mysql_insert_id +1 to add a new ID or is there a call for adding a new unique ID?
Hi - I found the mysql_insert_id function to retrieve the last auto generated ID.
Should I be using mysql_insert_id +1 to add a new ID or is there a call for adding a new unique ID?
If your id field is set to auto increment, you don't have to add an ID at all. It will be incremented and added automatically.
AUTO_INCREMENT in MySQL does exactly what it sounds like. When you insert a new record it will automatically generate a new ID for you. You do not need a separate call.
Insert a new record and set the auto-increment column to NULL, or just omit it entirely (which is implicitly setting it to NULL - it has the same result). The column will be set to the next auto-increment value instead of NULL.
Using NULL for id:
INSERT INTO `database`.`table` (`id`, `user`, `result`) VALUES (NULL, 'Alice', 'green')");
OR not specifying id at all:
INSERT INTO `database`.`table` (`user`, `result`) VALUES ('Alice', 'green')");
Either way works just fine, more of a preference but personally I chose the second as its less typing.