php整型溢出有关问题

WBOY
Release: 2016-06-13 10:19:53
Original
1056 people have browsed it

php整型溢出问题
今天在写个程序 发现插入的MYSQL数据和POST的不一致 调试发现是数据转换时发生的错误

比如一个手机号 我想转换为int 这个手机号是任意字符 不是手机号则false 以前都是用intval
比较好用 比如
if(intval('13688017729'))
...然后继续 

发现
echo intval('13688017729');这句不等于13688017729了 

我的PDO是这样搞的 帮我看下怎么完善下 修改这个BUG

if (array_key_exists($key, $data)) {
  if (isset($data[$key])) {
  if ($table_column[$key]["d_type"] == "decimal" || $table_column[$key]["d_type"] == "double") {
  $newdata[$key] = doubleval($data[$key]);
  } elseif ($table_column[$key]["d_type"] == "int" || $table_column[$key]["d_type"] == "smallint" || $table_column[$key]["d_type"] == "tinyint" || $table_column[$key]["d_type"] == "mediumint" || $table_column[$key]["d_type"] == "bigint") {
  $newdata[$key] = intval($data[$key]);
  } elseif ($table_column[$key]["d_type"] == "float") {
  $newdata[$key] = floatval($data[$key]);
  } else {
  $newdata[$key] = $data[$key];
  }
  }
  }

------解决方案--------------------
不能这样做!手机号码已经超出整型数的表示范围了
要检查他是否全由数字组成可用 is_numeric 函数或正则
------解决方案--------------------
出现错误是因为数据库长度溢出 而不是因为你的类型转换.所以如果想存储手机号的话可以将字段类型修改成bigint

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!