views:

46

answers:

1

i ave been having trouble with a simple select sql query. using php PDO.

for some reason the rowcount returns 1 but fetch and fetchall both return false; to me that means the execute failed or the query returned no results which would have a rowcount of 0. my sql syntax as far as i can tell is fine.

here is the sql query

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

here is the code.

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());
    }
A: 

Looks like the problem is:

$this->selectCustomerAddressStatement->closeCursor();

.. which is being called after the row count. Closing the cursor probably releases the results of the statement which is why fetching returns nothing. Move that call to the end when you're done with fetching the data.

Rob Olmos
omg i can't believe i never noticed that. sitting there for 2 hours thinking wtf. thanks a million.
Ryan