dongzhouji4021
dongzhouji4021
2015-12-05 18:28

准备并执行一种方法

已采纳

I need PDO's prepare() and execute() in one method... but it doesn' work. Environment: 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>';
        }
    }
}

Calling method

$sql = "SELECT * FROM table";
$dbh->xquery($sql) OR die($dbh->error);

Any suggestions appritiated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doushang1890 doushang1890 6年前

    Found the bug... xquery() should return $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;
    }
    
    点赞 评论 复制链接分享