Counting strings reached PDO

3

I'm doing a query and need to count the number of rows returned, I'm not usually accustomed to doing this in PDO, follow my code below.

$sqlCODCEL = $conn->prepare("SELECT * FROM tbl_CELULAS WHERE TXT_CODIG_SECUR = :codCEL");
$sqlCODCEL->bindParam(":codCEL", $lbl_CODCELULA);
$linha = $sqlCODCEL->fetchAll();
$count = count_chars($linha);

error_log($count);
    
asked by anonymous 24.08.2015 / 13:47

2 answers

2

In order for you to return the amount of affected rows, you have to use rowCount(); . Home Probably $linhasafetadas = $sqlCODCEL->rowCount() will solve your problem.

An example:
<?php
/* Delete all rows from the FRUIT table */
$del = $dbh->prepare('DELETE FROM fruit');
$del->execute();

/* Return number of rows that were deleted */
print("Return number of rows that were deleted:\n");
$count = $del->rowCount();
print("Deleted $count rows.\n");
?>

Return:

  

Return number of rows that were deleted:   Deleted 9 rows.

Source: php.net

    
24.08.2015 / 13:58
4

Use the method rowCount () it returns the rows affected by a DML (insert, update delete ) in some databases also returns the rows of a select, in mysql it works correctly.

$linhas =$sqlCODCEL->rowCount();

Related:

How to get the number of rows from a select with SQL Server.

    
24.08.2015 / 13:53