mysql_insert_id()返回0 [英] mysql_insert_id() returns 0

查看:464
本文介绍了mysql_insert_id()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多标题相同的主题.但是大多数情况是将查询插入了错误的位置.但是我认为我说对了. 所以问题是,即使将数据插入db中,我仍然会得到0. 有人知道我可能错了的答案吗?

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?

这是我的代码:

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)

推荐答案

根据

According to the manual mysql_insert_id returns:

由上一个查询为AUTO_INCREMENT列生成的ID 成功,如果先前查询未生成AUTO_INCREMENT,则为 0 值,如果未建立MySQL连接,则为FALSE.

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.

由于它没有给您false并且没有正确的数字,因此它表示查询的表没有生成自动增量值.

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

解决方案:

  1. 确保它具有一个auto_increment字段
  2. 还提供链接:$waarde = mysql_insert_id($this->db);

这篇关于mysql_insert_id()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆