简单写个TP上获取 Exception 上的获取mysql错误信息跟code
try{ $CdkeyModel = User::create($insertData); //将会报错的代码 }catch(Exception $e){ $array = Exception_error($e->getData(),"mysql_error");// $array['code'] == 1062 and die("循环创建发生重复"); }
//获取Exception错误信息 function Exception_error($data,$name){ foreach ($data as $key=>$value){ switch ($name){ case "mysql_error": if($key == "PDO Error Info"){ $array['code'] = $value['Driver Error Code']; $array['message'] = $value['Driver Error Message']; return $array; } break; } } }