0

我正在尝试使用 pdo 从 SQL 查询中获取一个数组,例如我发送给它的方法$connection->selectFrom('Person',array(1,2));

当我尝试获取结果时,它返回一个空数组,这是我的代码:

public function selectFrom($table,$indexes){
    try{
        $pdo=$this->getPdo();
        // HERE I GET ALL THE COLUMN NAMES FROM THE TABLE I RECEIVE
        $columns = $this->getColumnNames($table);
        $finals = array();
        // IN THIS CICLE I GET THE COLUMNS THAT MATCH THE INDEXES I RECEIVE
        for($i=0;$i<count($indexes);$i++){
            $finals[$i] = $columns[$indexes[$i]];
        }
        // FROM HERE I GET THE QUERY STATEMENT WICH IS SELECT column1,column2 from $table 
        $query = $this->getSelectSQL($table, $finals);

        // ALL OF THE ABOVE WORKS BUT HERE IT STOPS WORKING
        $results =  $pdo->query($query);

        return $results;

    }catch(PDOException $ex){
        echo "EXCEPTION ".$ex;
    }
}
4

2 回答 2

1

感谢@Cymbals 的回答,最终代码如下:

public function selectFrom($table,$indexes){
    try{
        $pdo=$this->getPdo();

        $columns = $this->getColumnNames($table);
        $finals = array();

        for($i=0;$i<count($indexes);$i++){
            $finals[$i] = $columns[$indexes[$i]];
        }

        $query = $this->getSelectSQL($table, $finals);
        $query.= " WHERE Available = 1";

        $stmt = $pdo->prepare($query);

        $stmt->execute();


        $results = $stmt ->fetchAll();
        return $results;            

    }catch(PDOException $ex){
        echo "EXCEPTION ".$ex;
    }
}
于 2012-06-14T16:38:03.450 回答
0

得到查询后试试这个,准备并执行

      $stmt = $pdo->prepare($query);
      var_dump($stmt);// if the prepare fails or sql query is messed up it will give false
      $results = $stmt->execute();
      return $results;
于 2012-06-14T16:09:28.863 回答