dongzhouji4021 2015-12-05 18:28
浏览 37
已采纳

准备并执行一种方法

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 2015-12-05 18:39
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料