Perl/MySQL: last_insert_id returns 0

542 views Asked by At

last_insert_id returns for the newly inserted row.

Everything is working just fine with Perl 5.18.

From the forums I understand that it might be because of some loosing connection. But can't see anything in the trace.

Here is the code sample:

  $self->_dbx->query(q{DROP TABLE IF EXISTS user_tbl;});

  $self->_dbx->query(q{
    CREATE TABLE user_tbl(
      user_id INT NOT NULL AUTO_INCREMENT,
      user_name VARCHAR(100) NOT NULL,
      PRIMARY KEY ( user_id )
    );
  });

  $self->_dbx->query(q{
    INSERT INTO user_tbl 
              (user_name)
      VALUES  ('DUMMY1');
  });

  print Dumper $self->_dbx->last_insert_id(undef, undef, undef, "user_id" ) or warn "Warn: Can not get it?";

  print Dumper $self->_dbx->query(q{
    SELECT * FROM user_tbl
      WHERE user_name= 'DUMMY1';
  })->hash;

DBI_TRACE:

    <- ping= ( 1 ) [1 items] at Driver.pm line 28
    <- prepare('
    CREATE TABLE user_tbl(
      user_id INT NOT NULL AUTO_INCREMENT,
      user_name VARCHAR(100) NOT NULL,
      PRIMARY KEY ( user_id )
    );
  ')= ( DBI::st=HASH(0x70f4468) ) [1 items] at Simple.pm line 168
    <- execute= ( '0E0' ) [1 items] at Simple.pm line 187
    <- finish= ( 1 ) [1 items] at Simple.pm line 286
    <- FETCH('Active')= ( 1 ) [1 items] at Connector.pm line 129
    <- FETCH('Active')= ( 1 ) [1 items] at Connector.pm line 129
    <- ping= ( 1 ) [1 items] at Driver.pm line 28
    <- prepare('
    INSERT INTO user_tbl 
              (user_name)
      VALUES  ('DUMMY1');
  ')= ( DBI::st=HASH(0x70f47c8) ) [1 items] at Simple.pm line 168
    <- execute= ( 1 ) [1 items] at Simple.pm line 187
    <- finish= ( 1 ) [1 items] at Simple.pm line 286
    <- FETCH('Active')= ( 1 ) [1 items] at Connector.pm line 129
    <- FETCH('Active')= ( 1 ) [1 items] at Connector.pm line 129
    <- ping= ( 1 ) [1 items] at Driver.pm line 28
    <- last_insert_id(undef, undef, ...)= ( '0' ) [1 items] at Simple.pm line 216

STDOUT:

$VAR1 = '0';
$VAR1 = {
          'user_id' => 1,
          'user_name' => 'DUMMY1'
        };

Versions:

  • Perl => 5.22
  • DBD::mysql => 4.043
  • MySQL Server 5.6
0

There are 0 answers