php保存數(shù)據(jù)到mysql
打算在dao層進(jìn)行數(shù)據(jù)入庫(kù)前的清理,比如varchar進(jìn)行trim,int進(jìn)行intval。
有一天突然想起,php intval的取值范圍與mysql的int類(lèi)型一樣嗎?
查了一下,不一樣……
http://php.net/manual/en/function.intval.php
http://dev.mysql.com/doc/refman/5.1/zh/column-types.html#numeric-types
php intval的取值范圍:與操作系統(tǒng)相關(guān),32位系統(tǒng)上為-2147483648到2147483647,64位系統(tǒng)上為-9223372036854775808到9223372036854775807。
mysql int取值范圍:與操作系統(tǒng)無(wú)關(guān),為-2147483648到2147483647,無(wú)符號(hào)為0到4294967295。
mysql bigint取值范圍:與操作系統(tǒng)無(wú)關(guān),為-9223372036854775808到9223372036854775807,無(wú)符號(hào)為0到18446744073709551615。
所以下面的代碼是錯(cuò)誤的:
復(fù)制代碼代碼如下:
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;
}
}
解決辦法:還在想,準(zhǔn)備用正則表達(dá)式。