2010-09-07 106 views
1

我一直有一个简单的选择sql查询的麻烦。 使用php PDO。PHP PDO提取错误。 rowcount = 1但fetchall返回false;

由于某种原因,rowcount返回1,但是提取和fetchall都返回false; 对我来说,这意味着执行失败或查询返回没有行数为0.我的sql语法,据我所知可以罚款。

这里是SQL查询

SELECT * FROM CustomerAddress WHERE ".CustomerAddress::custAddressID." = :".CustomerAddress::custAddressID." LIMIT 1 

这里是代码。

try{ 
     if($this->selectCustomerAddressStatement->execute(array(CustomerAddress::custAddressID => $addressID))) 
     { 
      if($this->selectCustomerAddressStatement->rowCount() == 1) 
      { 
       $this->selectCustomerAddressStatement->closeCursor(); 
       if($temp = $this->selectCustomerAddressStatement->fetch()) 
       { 
        $customerAddress = new CustomerAddress($temp); 
        if($customerAddress->getCustAddressID() == $addressID) 
        { 
         return $customerAddress; 
        }else{ 
         throw new Exception("The Customer Address Retrieved was not what was Asked."); 
        } 
       }else{ 
        throw new Exception("Failed to retrieve Result set."); 
       } 
      }else{ 
       throw new Exception("Statement Returned To Many Results."); 
      } 
     }else{ 
      throw new Exception("Failed to Execute Statement."); 
     } 
    }catch(Exception $e) { 
     $this->selectCustomerAddressStatement->closeCursor(); 
     throw new Exception("Customers:: selectCustomerAddress - ".$e->getMessage()); 
    } 

回答

1

貌似问题是:

$this->selectCustomerAddressStatement->closeCursor(); 

..这正行计数后调用。关闭游标可能会释放语句的结果,这就是为什么fetching不返回任何内容。完成提取数据后,将该调用移到最后。

+1

omg我不敢相信我从来没有注意到这一点。坐在那里2个小时思考跆拳道。太感谢了。 – Ryan 2010-09-07 06:57:38