mysql float字段类型数据查询为空问题

作者:matrix 发布时间:2021 年 12 月 28 日 分类:mysql PHP

结论

不要用float、double类型存储浮点数。改用decimal字段类型

过程

之前是知道浮点数最好不要用float类型做存储,手上遇到老项目使用就正好是float字段存储的体重数据,比如51.6这种。

普通的查询没问题,个别数据就出现查询为空的问题。后来发现都是浮点类型数据,排查框架的sql日志到PDO的参数绑定找遍了都没找到根源。还以为是PDO扩展的data_type出错,因为内部sql执行时浮点数的参数绑定是使用PDO::PARAM_STR

$this->PDOStatement->bindValue(':ThinkBind_1_', 51.6, PDO::PARAM_STR)

虽然字段设置了精度float(10,2),但是依然有查询为空出现。这就是float精度导致的问题。

吐槽

TP5.1 sql日志输出不准确,和实际执行的不一致!

sql输出日志为where wi=51.6,实际上执行是where wi="51.6",这也增加了排查的难度。

办法

  1. 浮点数查询使用like
  2. 使用函数比如oncat(wi)=51.6,或者format(wi,2) = format(51.6 ,2)
  3. 使用decimal字段类型

参考:

https://www.cnblogs.com/powerwu/articles/8465031.html

https://blog.csdn.net/luccs624061082/article/details/84286253

suning云盘解析源码[PHP]

作者:matrix 发布时间:2015 年 10 月 29 日 分类:PHP

苏宁云盘的下载速度还是不错的,之前获取下载地址很简单。现在必须要求用户登录才能办到,难度也是增加不少。还好suning没做绝。GOOD LUCK!

提示: 回复可见网盘地址

源码

阅读剩余部分 »

php implode函数 多维数组

作者:matrix 发布时间:2015 年 3 月 19 日 分类:PHP

PHP implode()[别名join]的作用是将数组元素拼接成一个字符串。
$arr=array('a','b',array('1','2'),'c'); //二维数组
$s=implode(',',$arr);
//返回a,b,Array,c
但是遇到上面这种多维数组这样的implode()就没办法处理。
I was a little worried about the multi-dimensional array implodes listed here, as they are using 'for' loops, which is bad programming practice as arrays are not always nice and neat.

I hope this helps
好在早有解决方案:

<?php  
function multi_implode($glue, $pieces)  
{  
    $string='';  

    if(is_array($pieces))  
    {  
        reset($pieces);  
        while(list($key,$value)=each($pieces))  
        {  
            $string.=$glue.multi_implode($glue, $value);  
        }  
    }  
    else  
    {  
        return $pieces;  
    }  

    return trim($string, $glue);  
}  

说明:和implode()使用参数一样。multi_implode(字符, 数组)
参考:
http://php.chinaunix.net/manual/zh/function.implode.php#94688
http://g.xker.com/97041.html
http://blog.sina.cn/dpool/blog/s/blog_50a1e1740101aet6.html?vt=4