float 在 mysql 数据库中不起作用
我正在使用 $_GET['var'] 获取变量,然后将其与数据库中的变量进行比较。变量是 1.1,var 在数据库上设置为“float”,所以我知道它可以处理小数,但是当我将它们与下面的代码进行比较时,我什么也没得到。
include 'connect.php';
$sql=mysql_query("SELECT * FROM table WHERE stuff='$stuff'");
while ($row=mysql_fetch_assoc($sql)) {
$start=$row['start'];
}
echo $start; //nothing happens
i am using $_GET['var'] to get a variable then compare it with a variable in my database. the variable is 1.1 the var is set to "float" on the database so i know it can handle decimals but when i compare them with the code below i get nothing.
include 'connect.php';
$sql=mysql_query("SELECT * FROM table WHERE stuff='$stuff'");
while ($row=mysql_fetch_assoc($sql)) {
$start=$row['start'];
}
echo $start; //nothing happens
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知
float
类型并不精确。它不会向您显示实际值,因此您保存的1.1
可能不是存储的实际值。尝试将字段设置为decimal
并指定长度,例如10,1
,其中 10 是最大位数(精度),1 是位数小数点右侧(刻度)。它应该可以执行诸如stuff='1.1'
或stuff=1.1
之类的查询。From what I know
float
type isn't precise. It doesn't show you that actual value so1.1
that you saved may not be the actual value stored. Trying setting your field asdecimal
and give it a length of say,10,1
where 10 is the maximum number of digits (the precision) and 1 is the number of digits to the right of the decimal point (the scale). It should work doing query likestuff='1.1'
orstuff=1.1
.不要使用
float
(即使在表中插入 1.1,float 类型的实际值也不是 1.1,而是类似 1.100000023841858 的值)。在数据库中将其更改为double
(或十进制)Don't use
float
( even if you insert 1.1 into the table, the actual value for float type is not 1.1, but something like 1.100000023841858) . Change it todouble
in database (or decimal)WHERE stuff = '$stuff'
是字符串比较。像这样比较数字
WHERE stuff = '$stuff'
is a String comparison.Compare number like so
您可能看不到任何输出,因为您的回显位于循环之外。
变量 $start 的范围将限制在循环内。
You might not be seeing any output because your echo is outside the loop.
The scope of your variable $start would be confined to the loop.
将 stuff 字段更改为 DOUBLE 类型。
那么,
这应该是sql查询
Change the stuff field to DOUBLE type.
Then,
this should be the sql query