mysql_insert_id() returns 0

前提是你 提交于 2019-11-27 05:11:31

According to the manual mysql_insert_id returns:

The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.

Since it does not give you false and not the correct number it indicates that the queried table didn't generate an auto-increment value.

There are two possibilities I can think of:

  1. Your table doesn't have an auto_increment field
  2. Since you doesn't provide the link to the mysql_insert_id() but using a link with mysql_query() it might not be the correct table that's queried when retrieving the last inserted id.

Solution:

  1. Make sure it has an auto_increment field
  2. Provide the link aswell: $waarde = mysql_insert_id($this->db);

It is possible that your INSERT query was not successful - e.g., maybe you were trying to insert duplicate data on a column whose data must be unique?

If the id is indeed set to auto increment and still get '0' as your response do a column and value count i experienced this only later on I noticed a number of my column count did not match values count.

Codeigniter has an odd behaviourd when calling mysql_insert_id(). The function returns 0 after the first call. So calling it twice will return 0.

Use a variable instead of calling the function more times:

$id = mysql_insert_id();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!