我需要在一个方法中使用PDO的prepare()和execute()。。。但它不起作用。环境:IIS 10/SQL Server 2014
class dbh extends PDO {
...
public function xquery($sql){
if(($sth = $this->prepare($sql)) === false){
$error_arr = $this->errorInfo();
$this->error .= '<span title="error_code:'.$error_arr[0].'">(prepare) '.$error_arr[1].':'.$error_arr[2].'</span>';
}
if($sth->execute() === false){
$error_arr = $sth->errorInfo();
$this->error .= '<span title="error_code:'.$error_arr[0].'">(execute) '.$error_arr[1].':'.$error_arr[2].'</span>';
}
}
}
调用方法
$sql = "SELECT * FROM table";
$dbh->xquery($sql) OR die($dbh->error);
欢迎提出任何建议!
发现错误。。。xquery()应返回$sth
public function xquery($sql){
if(($sth = $this->prepare($sql)) === false){
$error_arr = $this->errorInfo();
$this->error .= '<span title="error_code:'.$error_arr[0].'">(prepare) '.$error_arr[1].':'.$error_arr[2].'</span>';
}
if($sth->execute() === false){
$error_arr = $sth->errorInfo();
$this->error .= '<span title="error_code:'.$error_arr[0].'">(execute) '.$error_arr[1].':'.$error_arr[2].'</span>';
}
if(empty($this->error))
return $sth;
}