thinkphp不会识别unsigned int数据类型,会导致sql执行错误
2017-03-28 14:23
561 查看
数据库中是 unsigned int, 支持40多亿, 但用tp进行查询的时候, where方法会自动把大于2147483647的(有符号int最大值) 转化成 2147483647
print_r($condition);
$Mode->where($condition)->find();
echo $Mode->getlastSql();
结果:
Array
(
[sid] => 2147645095
[type] => icili
)
SELECT * FROM `al_qitadianying` WHERE ( `sid` = 2147483647 ) AND ( `type` = 'icili' ) LIMIT 1
sid 字段是 unsigned in
---------------------------------------------
下面是我自己的测试结果
我测试了,
$data['userID'] = "davidhuang";
$data['sysID'] = 2147645095;
$data['loginTime'] = datetime();
$data['loginStatus'] = 0;
$data['validStatus'] = 0;
$result = M("loginLog")->add($data);
dump(M()->getLastSql());
生成的sql是
INSERT INTO `login_log` (`userID`,`sysID`,`loginTime`,`loginStatus`,`validStatus`) VALUES ('davidhuang',-2147322201,'2013-11-19 09:58:44',0,0)
sysID的类型是
int(11) UNSIGNED
应该是个bug。
测试TP版本 3.1.3
看下tp的源码:
/**
* 数据类型检测
* access protected
* @param mixed $data 数据
* @param string $key 字段名
* return void
*/
protected function _parseType(&$data,$key) {
$fieldType = strtolower($this->fields['_type'][$key]);
if(false !== strpos($fieldType,'int')) {
$data[$key] = intval($data[$key]);
}elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){
$data[$key] = floatval($data[$key]);
}elseif(false !== strpos($fieldType,'bool')){
$data[$key] = (bool)$data[$key];
}
}
它用的是intval,转换后的最大值是2147483647。
API说明:
intval($var, $base)
Get the integer value of a variable
link http://www.php.net/manual/en/function.intval.php
@param var mixed
The scalar value being converted to an integer
@param base int[optional]
The base for the conversion
return int The integer value of var on success, or 0 on failure. Empty arrays and objects return 0, non-empty arrays and objects return 1.
The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.
Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.
print_r($condition);
$Mode->where($condition)->find();
echo $Mode->getlastSql();
结果:
Array
(
[sid] => 2147645095
[type] => icili
)
SELECT * FROM `al_qitadianying` WHERE ( `sid` = 2147483647 ) AND ( `type` = 'icili' ) LIMIT 1
sid 字段是 unsigned in
---------------------------------------------
下面是我自己的测试结果
我测试了,
$data['userID'] = "davidhuang";
$data['sysID'] = 2147645095;
$data['loginTime'] = datetime();
$data['loginStatus'] = 0;
$data['validStatus'] = 0;
$result = M("loginLog")->add($data);
dump(M()->getLastSql());
生成的sql是
INSERT INTO `login_log` (`userID`,`sysID`,`loginTime`,`loginStatus`,`validStatus`) VALUES ('davidhuang',-2147322201,'2013-11-19 09:58:44',0,0)
sysID的类型是
int(11) UNSIGNED
应该是个bug。
测试TP版本 3.1.3
看下tp的源码:
/**
* 数据类型检测
* access protected
* @param mixed $data 数据
* @param string $key 字段名
* return void
*/
protected function _parseType(&$data,$key) {
$fieldType = strtolower($this->fields['_type'][$key]);
if(false !== strpos($fieldType,'int')) {
$data[$key] = intval($data[$key]);
}elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){
$data[$key] = floatval($data[$key]);
}elseif(false !== strpos($fieldType,'bool')){
$data[$key] = (bool)$data[$key];
}
}
它用的是intval,转换后的最大值是2147483647。
API说明:
intval($var, $base)
Get the integer value of a variable
link http://www.php.net/manual/en/function.intval.php
@param var mixed
The scalar value being converted to an integer
@param base int[optional]
The base for the conversion
return int The integer value of var on success, or 0 on failure. Empty arrays and objects return 0, non-empty arrays and objects return 1.
The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.
Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.
相关文章推荐
- java 执行sql错误 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。参数 1 (""): 数据类型 0x38 未知
- access中sql语句执行错误 “标准表达式中数据类型不匹配”
- 解决SQL将varchar值转换为数据类型为int的列时发生语法错误
- PostgreSQL源码分析(2)– 常用数据类型/SQL语句的解释和执行
- Oracle Class6-1. PL/SQL 简介(数据类型,逻辑比较,控制结构,错误处理)
- 解决SQL将varchar值转换为数据类型为int的列时发生语法错误
- EntityManager转换为Hibernate的Session执行sql获取Map类型的数据集合
- 解决错误:sql_variant is incompatible with xml (ASP.NET 2.0 / XML数据类型 )
- dbutils 执行sql返回的数据类型
- SQLserver不是可以识别的内置函数名称"的错误,通过set statistics time on能得到SQL语句的执行时间精确到毫秒.
- 2016/05/13 thinkphp 3.2.2 ① 数据删除及执行原生sql语句 ②表单验证
- 业务逻辑>数据层>SqlDataSource>处理sql命令执行错误
- C#执行sql语句抛出“标准表达式中数据类型不匹配”
- 关于sql条件语句where id in (@参数)执行报错问题(转换成数据类型 int 时失败)(
- sqlserver数据类型转换(将 nvarchar 转换为数据类型 numeric 时出现算术溢出错误)
- sql 将 varchar 值转换为数据类型为 int 的列时发生语法错误 的解决办法
- SQL中查询ORCALCE库数据 报错误:从数据类型 DBTYPE_DBTIMESTAMP 转换为 datetime 时出错
- SQLserver不是可以识别的内置函数名称"的错误,通过set statistics time on能得到SQL语句的执行时间精确到毫秒.
- (SQL)将 expression 转换为数据类型 int 时发生算术溢出错误 2种快速处理方法
- ThinkPHP--删除数据及原生sql语句执行