Home > php教程 > php手册 > php数据入库前清理 注意php intval与mysql的int取值范围不同

php数据入库前清理 注意php intval与mysql的int取值范围不同

WBOY
Release: 2016-06-21 08:55:58
Original
1211 people have browsed it

php保存数据到mysql
打算在dao层进行数据入库前的清理,比如varchar进行trim,int进行intval。
有一天突然想起,php intval的取值范围与mysql的int类型一样吗?
查了一下,不一样……
http://php.net/manual/en/function.intval.php
http://dev.mysql.com/doc/refman/5.1/zh/column-types.html#numeric-types
php intval的取值范围:与操作系统相关,32位系统上为-2147483648到2147483647,64位系统上为-9223372036854775808到9223372036854775807。
mysql int取值范围:与操作系统无关,为-2147483648到2147483647,无符号为0到4294967295。
mysql bigint取值范围:与操作系统无关,为-9223372036854775808到9223372036854775807,无符号为0到18446744073709551615。
所以下面的代码是错误的:

复制代码 代码如下:


public function insert($data)
{
if(isset($data['content'])&&!empty($data['content']))
{
$data_for_query['content'] = trim($data['content']);
}
else
{
return false;
}
if(isset($data['user_id'])&&!empty($data['user_id']))
{
$data_for_query['user_id'] = intval($data['user_id']);
}
else
{
return false;
}
$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
$this->db->query($sql);
$id = $this->db->lastInsertId();
if(empty($id))
{
return false;
}
else
{
return $id;
}
}


解决办法:还在想,准备用正则表达式。



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template