0

我有一个数据库,用于存储长度为 17 个字符的车辆的 VIN 号码(存储为 VID)。我正在尝试递增到下一个 VID 以将其显示在可以在数据库中添加新车的页面上。数据库中的 VID 字段类型为 bigint(17) 并设置了 auto_increment。

$incr_query = "SELECT MAX(VID) FROM Vehicle";
$incr_result = mysql_query($incr_query);
$row = mysql_fetch_assoc($incr_result);
$vid = $row["MAX(VID)"] + 1;

输出:

print var_dump{$row);//array(1) { ["MAX(VID)"]=> string(17) "12345678123456788" }
print gettype($vid);    //double
print $vid;             //1.2345678123457E+16
print number_format(++$vid, 0, '.', '');       //12345678123456788
print number_format(--$vid, 0, '.', '');       //12345678123456788
print number_format($vid - 5, 0, '.', '');     //12345678123456784 

我不确定这里发生了什么。根据PHP 浮动手册

"The size of a float is platform-dependent, although a maximum of ~1.8e308 with 
a precision of roughly 14 decimal digits is a common value"

编辑:

所以使用 bcadd,这些是输出:

$vid = bcadd($vid, '1', 0); 
print "\n".$vid;                              //12345678123456789
print "\n".number_format($vid, 0, '.', '');   //12345678123456788
4

1 回答 1

0

VID 号码可能看起来像“双”或“整数”或其他任何东西,但它不是。将其存储为字符串。查看Vimeo上的 Skeet 以获得解释。它从 6:10 左右开始,到 10:00 左右结束。

A bigint in the DB might do, but a double is a floating point and floating points are not intended to be used as discrete numbers; that's why they're floating points. I would go with string (varchar) all the way, up to the database. Using a bigint would encourage you/other developers to keep treating it as a "number" wich, in fact, it isn't (exactly).

于 2012-04-05T00:50:50.943 回答