Pedramch Pedramch - 1 month ago 9
MySQL Question

Laravel : Function Always Insert One Number To DB In LOOP

I want create some random codes based on miliseconds.
when i use "var_dump" it shows my function is working correctly and too many $result numbers are created. but when i use ORM INSERT Statement to save them into my database , it always SAVE : 2147483648 in database and it seems it's not depended on my $result !!!!!
here is my code :

public function Timer($Number)
{
$i=0;
while ($i<$Number)
{
$microtime = microtime(true);
$milliseconds = sprintf("%02d", ($microtime - floor($microtime)) * 96 * 62 * 23 * 43);
$result=date('His'. $milliseconds, $microtime)."";
$result=substr($result,1,11);
DB::table('form')->insert([
'Number' => $result,
'IsValid' => false
]);
$i++;
}
}

Answer

2147483647 is maximum number for signed INT. So, try to use unsigned BIGINT or VARCHAR to store the value.

Or you could use another way to build random codes, like str_random() or something similar.