strstr() strpos() 获取db报错,判断报错中是否包含字符串,判断错误类型

 

model中直接获取添加公司的错误.(公司名称不能重复)

 

$enterprise_id = $this->add($enterprisedata );

$err = $this->getDbError();

$err =="1062:Duplicate entry 'aaa' for key 'enterprise_name'\n [ SQL\u8bed\u53e5 ] : INSERT INTO `t_enterprise` (`enterprise_name`,`enterprise_addtime`,`enterprise_account_id`) VALUES ('aaa','2016-06-11 16:39:20','23666')"

 

$num = strstr($err,'Duplicate');  //字符串中,包含字符串.返回从Duplicate开始到结尾的字符串
var_dump($num);

$num = strpos($err,'Duplicate');   //字符串中,出现Duplicate的位置.5

都是,在字符串中(第一个参数),,,,查找出现的字符串(第二个参数)

 

$num =  explode('Duplicate',$err); //使用xxx字符串,分割字符串(第二个参数)

posted @ 2016-06-11 16:54  小庄啊  阅读(294)  评论(0编辑  收藏  举报