-1

This query returns 19 as lastInsertId() instead of 000000019. Why?

try {
        $stmt = $conn->prepare("INSERT INTO TABLE(NAME) VALUES
        (:name)");
        $query_params = array( 
            ':name' => $name
        ); 
    $stmt->execute($query_params);
    echo $conn->lastInsertId();
    $response["success"] = 1;
    } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    $response["success"] = 0;
    }

It's not a problem because when I insert into another table with the id of 19 it is inserted as 000000019. I just want to know where do the leading zeroes disappear. Haven't found anything about this on Google.

erdomester
  • 11,789
  • 32
  • 132
  • 234
  • That depends on your driver and table definition. Generally speaking, integers don't have leading zeros unless formatted this way to a string (text). – Ferdinand Beyer Aug 09 '14 at 08:07
  • maybe there is a zero fill on the column? – Kevin Aug 09 '14 at 08:11
  • Of course, the ID column is UNSIGNED ZEROFILL – erdomester Aug 09 '14 at 08:17
  • possible duplicate of [PHP MySQL PDO: how to preserve leading zeros of zerofill int columns](http://stackoverflow.com/questions/25010145/php-mysql-pdo-how-to-preserve-leading-zeros-of-zerofill-int-columns) – VMai Aug 09 '14 at 08:24

1 Answers1

0

That's because 000000019 is a string and it's being cast into an integer by the lastInsertId() method. This will remove all the preceding 0's.

Are you using a layer on top of the native mysqli/PDO objects as that may be doing the casting, we can check the source code to see if that's the case.

Jujhar Singh
  • 3,641
  • 5
  • 29
  • 38
  • 1
    lastInsertId() by itself does no casting and returns a string according to the docs: http://php.net/manual/en/pdo.lastinsertid.php – Ferdinand Beyer Aug 09 '14 at 08:10