您的位置:首页 > 编程语言 > PHP开发

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐