-1

I am creating admissions table in which stud_id column is unsigned bigint(250). I am apply query "alter table admission AUTO_INCREMENT = 2001923456789000; and in php coding i am fetching last index and adding 1 into it but i get wrong result plz give solution. result in exponential format.

$sql="select MAX(id) from com_result"; 
$result=mysql_query($sql) or die(mysql_error()); 
if($row=mysql_fetch_array($result)) { 
     $id=$row[0]; 
     $stud_id1=$id+1; 
     echo "<input type='text' class='form-control' id='id' name='id' readonly value=".$stud_id1.">"; 
}
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57

1 Answers1

0

You are basically hitting the maximum limit for an integer value in PHP. As corroborated by you in comment, the result of echo PHP_INT_MAX; on your server is: 2147483647. (so, basically a 32-bit build of PHP).

One option is to rebuild the PHP on your server with a 64-bit version. Check this answer to understand the difference in integer range for different builds: https://stackoverflow.com/a/2842548/2469308

Now, in your current case, the number obtained from MySQL: 2001923456789000 is basically overflowing, since it is much higher than the maximum allowed value for an integer. That is why you are seeing wrong results, because once the limit is crossed, PHP would go backwards and start from the lowest allowed negative integer value, and so on.. (in cycles)

Now, in order to solve this issue in a 32-bit PHP, you will have to explicitly typecast the result obtained from MySQL to a Float datatype. Floats can be used for integer values up to 2^53 + 1.

Try the following:

if($row=mysql_fetch_array($result)) { 
     $id = (float)$row[0];  // <-- Typecasting to float
     $stud_id1 = $id + 1; 
     echo "<input type='text' class='form-control' id='id' name='id' readonly value=".$stud_id1.">"; 
}
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
  • it gives result like 2.019273413028E+17 and last index of table e.g 201927341302805001 and i want next number 201927341302805002 – user11708206 Jul 14 '19 at 06:05
  • @user11708206 alternative solutions are available at: https://stackoverflow.com/questions/4427020/is-there-a-biginteger-class-in-php – Madhur Bhaiya Jul 14 '19 at 06:44