0

我在实施以下方法时遇到问题:

private function query($statement, $params = array(), $name = ""){

    try{
        $this->prepare($statement, $name);
    }catch (Exception $e){
        echo 'Exception: ',  $e->getMessage(), "\n";
    }   
    try{
        return $this->execute($name, $params);
    }catch (Exception $e){
        echo 'Exception: ',  $e->getMessage(), "\n";
    }
}

第二个 try-block 应该从另一个调用的方法返回值。我这样称呼它:

$resultset = $db->select('SELECT * FROM tabelle WHERE "ID" > $1', array(1));
var_dump($resultset);

并得到一个NULL。
如果我 var_dump 尝试块内的值,我会得到一个结果:

private function query($statement, $params = array(), $name = ""){

    try{
        $this->prepare($statement, $name);
    }catch (Exception $e){
        echo 'Exception: ',  $e->getMessage(), "\n";
    }   
    try{
        var_dump($this->execute($name, $params));
    }catch (Exception $e){
        echo 'Exception: ',  $e->getMessage(), "\n";
    }
}

数组(3){[“ID”]=>字符串(1)“2”[“名称”]=>字符串(13)“名称1”[“时间”]=>字符串(26)“2012-09-11 20:23:38.854918”} 空

我做错了什么?

编辑$db->select 和发布的方法之间的部分:

public function select($statement, $params = array(), $name){
  $this->setQueryType("select"); // just a setter
  return $this->query($statement, $params, $name);
}
4

1 回答 1

0

尝试存储execute的返回值,并在方法结束时返回,看看是否有效?

private function query($statement, $params = array(), $name = ""){
  $return = false;
  try{
    $this->prepare($statement, $name);
    $return = $this->execute($name, $params);
  }
  catch (Exception $e){
    echo 'Exception: ',  $e->getMessage(), "\n";
  }
  return $return;
}
于 2012-09-11T22:19:12.770 回答