prepare($s-6ren">
gpt4 book ai didi

php - 使用 php pdo 对 mysql 数据库调用存储过程后检查没有行 columnCount 不起作用

转载 作者:行者123 更新时间:2023-11-29 15:33:44 27 4
gpt4 key购买 nike

    $sql = "CALL pr_cartItemsForCustomer (:customerId)";        
$statement = $connection->prepare($sql);
$statement->bindValue(':customerId',$customerId);
$statement->execute();

if($statement->columnCount() > 0)

即使没有返回行,它也总是返回 5 列,检查没有行的正确方法是什么?我尝试了 rowCount 但没有成功。

该代码对于查询可以正常工作,但在调用返回查询的存储过程时则不行。

最佳答案

查找返回行数的正确方法是使用 count()在返回的 PHP 数组上。

$sql = "CALL pr_cartItemsForCustomer (:customerId)";        
$statement = $connection->prepare($sql);
$statement->bindValue(':customerId',$customerId);
$statement->execute();
$results = $statement->fetchAll();

echo count($results);

但是,请注意您的示例根本不需要它。如果您只想检查任何返回的行,则只需使用数组本身作为 bool 值。例如if($results) 了解更多信息 https://phpdelusions.net/pdo#count

$results = $statement->fetchAll();

if($results) {
foreach($result as $row) {
// ...
}
} else {
echo 'No records!';
}

关于php - 使用 php pdo 对 mysql 数据库调用存储过程后检查没有行 columnCount 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com