PHP PDO 获取错误。 rowcount = 1 但 fetchall 返回 false;
我在执行简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来问题是:
..在行计数之后被调用。关闭游标可能会释放语句的结果,这就是为什么获取不会返回任何内容的原因。当您完成获取数据后,将该调用移至末尾。
Looks like the problem is:
.. 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.