PHP pdo ,为什么不抛出异常?
程序员文章站
2022-03-29 18:01:40
...
如下代码:
/**
* 写入数据
*
* @param string $table
* @param array $data
* @return int
*/
public function insert($table ,$data){
try {
$sql = "INSERT INTO ".$this->table($table);
$columns = $values = '';
foreach( $data as $k=>$v ) {
$columns .= "`$k`,";
$values .= ":$k,";
}
$columns = rtrim($columns,',');
$values = rtrim($values,',');
$sql = $sql . " ($columns) VALUES ($values)";
$this->_init($sql ,$data);
unset($sql ,$columns ,$values);
return $this->insertId();
}catch( \PDOException $e ) {
$this->halt($e->getMessage() ,$this->getSql());
return false;
}
}
会生成如下SQL,
INSERT INTO `dba_tables` (`pid`,`sellerId`,`cardNum`,`processUnit`,`proDate`,`dateOfPro`,`shipDate`,`clinicName`,`doctor`,`created`,`doctorEmail`,`doctorMobile`,`treatTime`,`treatDesc`,`patient`,`patientEmail`,`remark`) VALUES (3,0,'2DFBD1-9E27-CFFD-6846-459CA9E9','第二车间',1460476800,1459008000,1463068800,'某医院','某医生',1460507802,'d@a.com','13333333333','','牙','某某某','c@a.com','asfasdf')
用PHP执行上面的语句,不出现在错误也不抛出异常。
已经开启了pdo的抛出异常模式
$this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
但是用phpmyadmin执行会出现如下错误
回复内容:
如下代码:
/**
* 写入数据
*
* @param string $table
* @param array $data
* @return int
*/
public function insert($table ,$data){
try {
$sql = "INSERT INTO ".$this->table($table);
$columns = $values = '';
foreach( $data as $k=>$v ) {
$columns .= "`$k`,";
$values .= ":$k,";
}
$columns = rtrim($columns,',');
$values = rtrim($values,',');
$sql = $sql . " ($columns) VALUES ($values)";
$this->_init($sql ,$data);
unset($sql ,$columns ,$values);
return $this->insertId();
}catch( \PDOException $e ) {
$this->halt($e->getMessage() ,$this->getSql());
return false;
}
}
会生成如下SQL,
INSERT INTO `dba_tables` (`pid`,`sellerId`,`cardNum`,`processUnit`,`proDate`,`dateOfPro`,`shipDate`,`clinicName`,`doctor`,`created`,`doctorEmail`,`doctorMobile`,`treatTime`,`treatDesc`,`patient`,`patientEmail`,`remark`) VALUES (3,0,'2DFBD1-9E27-CFFD-6846-459CA9E9','第二车间',1460476800,1459008000,1463068800,'某医院','某医生',1460507802,'d@a.com','13333333333','','牙','某某某','c@a.com','asfasdf')
用PHP执行上面的语句,不出现在错误也不抛出异常。
已经开启了pdo的抛出异常模式
$this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
但是用phpmyadmin执行会出现如下错误
回答已删除,见谅