[nycphp-talk] mysql_insert_id Strangeness

Hans Zaunere hans at
Tue Aug 5 12:29:09 EDT 2003

> using the mysql client, are you able to replicate the problem? meaning,
> do you get the right mysql_insert_id when performing inserts in the
> mysql client

An illustrative C program (below) shows the same behavior, so it doesn't seem to be anything with PHP.

And even the MySQL documentation says something different than the behavior, from:

"Note that mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value..."

As Carlos pointed out, this is a gross documentation ambiguity.  The statement above is false according to the actual behavior, since an INSERT that fails because of a UNIQUE index doesn't generate an AUTO_INCREMENT value, yet mysql_insert_id() doesn't return 0.  This seems like a new quirk, although I could be wrong.

I'm going to bounce this off the mysql guys and see what they say.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mysql/mysql.h"

MYSQL mysql;
MYSQL_RES *mysql_result;
MYSQL_ROW mysql_row;

char *links[6] = {"",

int main(void) {

   int i,R_linkid;
   char insert_string[255];



   for( i = 0; i < 6; ++i ) {
      printf("\nLink %d: %s\n", i,links[i]);
      sprintf(insert_string, "INSERT INTO ptips.links (linkid,link)


      R_linkid = mysql_insert_id(&mysql);

      printf("\nR_linkid: %d\n",R_linkid);


More information about the talk mailing list