0

I have an ID system with PHP. It generates a 12 digit ID this way:

// create a 12 digit ID
function createTwelveId(){
  $digit_1 = rand(0, 9);

  $digit_2 = rand(0, 9);

  $digit_3 = rand(0, 9);

  $digit_4 = rand(0, 9);

  $digit_5 = rand(0, 9);

  $digit_6 = rand(0, 9);

  $digit_7 = rand(0, 9);

  $digit_8 = rand(0, 9);

  $digit_9 = rand(0, 9);

  $digit_10 = rand(0, 9);

  $digit_11 = rand(0, 9);

  $digit_12 = rand(0, 9);

  $pre_twelve_id = $digit_1 . $digit_2 . $digit_3 . $digit_4 . $digit_5 . $digit_6 . $digit_7 . $digit_8 . $digit_9 . $digit_10 . $digit_11 . $digit_12;

  return $pre_twelve_id;
}

When called, it is like this:

$comment_ID = createTwelveId();

And then it is inserted into my database this way:

//create comment
  $sentence = $connection->prepare("
    INSERT INTO comments (ID)
    VALUES (:ID)
");
$sentence->execute(array(
  ':ID' => $comment_ID
));

But every time I check the result on the database, it always inserts this:

004294967295

Please notice on this column I have Zerofill and Unsigned values on, that's why the int has 2 zeros at the beginning. And when I remove the Zerofill and Unsigned values, it inserts this:

2147483647

It always does this. Isn't it supposed to insert a random int into the database instead of those 2 numbers?

How can I stop this from happening and instead of those 2, insert the real random characters?

Cesar Pc
  • 27
  • 5
  • 3
    The number is too big for your database column. 004294967295 = 2^32 – miken32 Apr 29 '21 at 20:01
  • Why not do `rand(100000000000,9999999999999)`? or `rand(1,9999999999999)` and zero pad. – user3783243 Apr 29 '21 at 20:04
  • 4
    Does this answer your question? [Incorrect Integer (2147483647) is inserted into MySQL?](https://stackoverflow.com/questions/10255724/incorrect-integer-2147483647-is-inserted-into-mysql) – El_Vanja Apr 29 '21 at 20:04
  • 1
    To resolve, you can make the column a BIGINT or save the value as a string. – miken32 Apr 29 '21 at 20:07

0 Answers0