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

WBOY
Freigeben: 2016-07-21 15:33:34
Original
1129 Leute haben es durchsucht

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;
}
}

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/322580.htmlTechArticlephp保存数据到mysql 打算在dao层进行数据入库前的清理,比如varchar进行trim,int进行intval。 有一天突然想起,php intval的取值范围与mysql的int类...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!