php mysql_insert_id() 返回 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8243503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:15:37  来源:igfitidea点击:

mysql_insert_id() returns 0

phpmysqlreturnmysql-insert-id

提问by user750079

I know there are a lot of topics with the same title. But mostly it's the query that's been inserted in the wrong place. But I think I placed it right. So the problem is, that I still get 0 even when the data is inserted in the db. Does someone knows an answer where I could be wrong?

我知道有很多主题具有相同的标题。但主要是查询插入了错误的位置。但我想我把它放在了正确的位置。所以问题是,即使将数据插入到数据库中,我仍然得到 0。有人知道我可能出错的答案吗?

here's my code:

这是我的代码:

mysql_query('SET NAMES utf8');
    $this->arr_kolommen = $arr_kolommen;
    $this->arr_waardes = $arr_waardes;
    $this->tabel = $tabel;
    $aantal = count($this->arr_kolommen);
    //$sql="INSERT INTO `tbl_photo_lijst_zoekcriteria` ( `PLZ_FOTO` , `PLZ_ZOEKCRITERIA`,`PLZ_CATEGORIE`)VALUES ('$foto', '$zoekje','$afdeling');";
    $insert = "INSERT INTO ".$this->tabel." ";
    $kolommen = "(";
    $waardes = " VALUES(";
    for($i=0;$i<$aantal;$i++)
    {
        $kolommen .=$this->arr_kolommen[$i].",";
        $waardes .="'".$this->arr_waardes[$i]."',";
    }
    $kolommen = substr($kolommen,0,-1).")";
    $waardes = substr($waardes,0,-1).")";
    $insert .=$kolommen.$waardes;   
    $result = mysql_query($insert,$this->db)  or die ($this->sendErrorToMail(str_replace("  ","",str_replace("\r\n","\n",$insert))."\n\n".str_replace(" ","",str_replace("\r\n","\n",mysql_error()))));
    $waarde = mysql_insert_id();

Thanks a lot in advance, because I have been breaking my head for this one for almost already a whole day. (and probably it's something small and stupid)

提前非常感谢,因为我几乎已经为这个感到头疼一整天了。(而且可能是小而愚蠢的东西)

回答by Marcus

According to the manualmysql_insert_id returns:

根据手册mysql_insert_id 返回:

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.

上一个查询成功时为 AUTO_INCREMENT 列生成的 ID,如果前一个查询没有生成 AUTO_INCREMENT 值,则为0,如果没有建立 MySQL 连接,则为 FALSE。

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

由于它没有给你false而不是正确的数字,它表明查询的表没有生成自动增量值。

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.
  1. 您的表没有 auto_increment 字段
  2. 由于您没有提供指向 mysql_insert_id() 的链接,而是使用了 mysql_query() 的链接,因此在检索最后插入的 id 时查询的表可能不是正确的表。

Solution:

解决方案:

  1. Make sure it has an auto_increment field
  2. Provide the link aswell: $waarde = mysql_insert_id($this->db);
  1. 确保它有一个 auto_increment 字段
  2. 也提供链接: $waarde = mysql_insert_id($this->db);

回答by ban-geoengineering

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?

您的 INSERT 查询可能不成功 - 例如,您可能试图在数据必须唯一的列上插入重复数据?

回答by kay

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.

如果 id 确实设置为自动递增并且仍然得到“0”,因为您的响应会执行列和值计数,我后来才遇到这种情况,我注意到我的一些列计数与值计数不匹配。

回答by user5475008

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.

Codeigniter 在调用 mysql_insert_id() 时有一个奇怪的行为。该函数在第一次调用后返回 0。所以调用它两次将返回 0。

Use a variable instead of calling the function more times:

使用变量而不是多次调用函数:

$id = mysql_insert_id();