在使用php pdo调用mysql数据库columnCount的存储过程后检查是否没有行不起作用

在使用php pdo调用mysql数据库columnCount的存储过程后检查是否没有行不起作用,php,mysql,pdo,Php,Mysql,Pdo,即使没有返回行,也总是返回5列,检查没有行的正确方法是什么?我尝试了rowCount,但没有成功 对于查询,该代码可以正常工作,但在调用返回查询的存储过程时则不行。查找返回行数的正确方法是在返回的PHP数组上使用 $sql = "CALL pr_cartItemsForCustomer (:customerId)"; $statement = $connection->prepare($sql); $statement->bindVal

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


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

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

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

    if($statement->columnCount() > 0)
但是,请注意,您的示例根本不需要它。如果您只想检查任何返回的行,那么只需将数组本身用作布尔值即可。e、 g.
if($results)
阅读更多


我在使用while($row=$statement->fetch(PDO::fetch_ASSOC))为什么?该语句首先是可遍历的,您不需要while/fetch循环,因此您的意思是:$results=$statement->fetchAll(PDO::fetch_ASSOC)$num_rows=计数($results);foreach($results as$row){/}是的,这或多或少就是我的答案。我只需要知道结果集是否为空
$sql = "CALL pr_cartItemsForCustomer (:customerId)";        
$statement = $connection->prepare($sql);    
$statement->bindValue(':customerId',$customerId);
$statement->execute();
$results = $statement->fetchAll();

echo count($results);
$results = $statement->fetchAll();

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