mysql_insert_id() returns 0
Asked Answered
C

4

10

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?

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)

Cate answered 23/11, 2011 at 14:4 Comment(3)
please show the table scheme. Do you have a auto increment table field?Tomaso
You're using a specific link identifier in your query ($this->db), but not for the insert_id. That could cause it to look to a whole other connection. "If the link identifier is not specified, the last link opened by mysql_connect() is assumed."Beatrizbeattie
@ Topener: Yes I have auto increment table field. ID Int(11) Nee Geen AUTO_INCREMENT @Beatrizbeattie and how could I solve the problem? (Thank you both for answeringàCate
D
16

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);
Diner answered 23/11, 2011 at 14:18 Comment(5)
The ID is an auto_increment field. But how could I solve it, if it's the 2nd problem?Cate
@Cate try call mysql_insert_id with $this->db as I've mentioned in the answerDiner
@ Marcus Sorry I overread it. But it worked. Thank you very much. I was so stupid to write instead of $this->db ; $insert, $result (with other words, all other vars, but forgot the $this). Thank you very much.Cate
What do you mean by $this->db? my code is this: mysql_query("INSERT INTO mytable (myColumn) values ('myValue');"); $id = mysql_insert_id();Rooftree
Please give the resource name also in case you get 0. eg. mysql_insert_id($connection)Kathiekathleen
E
0

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?

Engrave answered 2/2, 2015 at 11:31 Comment(0)
H
0

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.

Hyperbolic answered 22/7, 2015 at 7:53 Comment(0)
G
0

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();
Graecoroman answered 22/10, 2015 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.