PHP PDO 获取错误。 rowcount = 1 但 fetchall 返回 false;

发布于 2024-09-18 08:34:23 字数 1632 浏览 10 评论 0原文

我在执行简单的 select sql 查询时遇到了麻烦。 使用 PHP PDO。

由于某种原因,rowcount 返回 1,但 fetch 和 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());
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2024-09-25 08:34:23

看起来问题是:

$this->selectCustomerAddressStatement->closeCursor();

..在行计数之后被调用。关闭游标可能会释放语句的结果,这就是为什么获取不会返回任何内容的原因。当您完成获取数据后,将该调用移至末尾。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文