NYCPHP Meetup

NYPHP.org

[nycphp-talk] mysql_insert_id Strangeness

Hans Zaunere hans at nyphp.org
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: http://www.mysql.com/doc/en/mysql_insert_id.html

"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.

H





#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] = {"http://hans.zaunere.com",
                  "http://zaunere.com",
                  "http://hans.zaunere.com",
                  "http://nyphp.org",
                  "http://lists.nyphp.org",
                  "http://nyphp.org"};


int main(void) {

   int i,R_linkid;
   char insert_string[255];

   mysql_init(&mysql);

   mysql_real_connect(&mysql,"localhost","ptips","ptips--",NULL,0,NULL,0);

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

      mysql_real_query(&mysql,insert_string,sizeof(insert_string));

      R_linkid = mysql_insert_id(&mysql);

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

}







More information about the talk mailing list