Counting lines reached PDO

Asked

Viewed 6,065 times

4

I’m doing a query and need to count the number of returned lines, I’m not usually used 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);

2 answers

5

3


In order for you to get back the amount of affected lines, you have to use rowCount();.
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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.