Home  >  Article  >  Backend Development  >  Clean up php data before entering it into the database. Note that the int value range of php intval is different from that of mysql_PHP Tutorial

Clean up php data before entering it into the database. Note that the int value range of php intval is different from that of mysql_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:341158browse

PHP saves data to mysql
It is planned to clean up the data before entering it into the database at the dao layer, such as trim for varchar and intval for int.
One day I suddenly remembered, is the value range of php intval the same as the int type of mysql?
I checked, it’s different...
http://php.net/manual/en/function.intval.php
http://dev.mysql.com/doc/refman/5.1/ zh/column-types.html#numeric-types
The value range of php intval: depends on the operating system. It is -2147483648 to 2147483647 on 32-bit systems and -9223372036854775808 to 9223372036854775807 on 64-bit systems.
Mysql int value range: Independent of the operating system, it is -2147483648 to 2147483647, and unsigned is 0 to 4294967295.
The value range of mysql bigint: independent of the operating system, is -9223372036854775808 to 9223372036854775807, unsigned is 0 to 18446744073709551615.
So the following code is wrong:

Copy code The code is as follows:

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

Solution: Still thinking about it, prepare to use regular expressions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322580.htmlTechArticlephp saves data to mysql. It is planned to clean up the data before entering it into the database at the dao layer, such as trim for varchar and trim for int. intval. One day I suddenly remembered that the value range of php intval is the same as the int class of mysql...
Statement:
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