Prepared Statements - Number of Rows

后端 未结 4 1577
旧巷少年郎
旧巷少年郎 2021-01-04 06:15

I\'m learning about prepared statements and trying to work with a query that yields multiple rows of results. Right now, I\'m just trying to figure out how to determine the

4条回答
  •  孤街浪徒
    2021-01-04 06:52

    This works as of Feb 2020:

    $number_of_records = $stmt->rowCount();
    echo $number_of_records;
    

    From php.net manual:

    PDOStatement::rowCount() returns the number of rows affected by a DELETE, INSERT, or UPDATE statement.

    Here is an example from their website:

    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");
    ?>
    

    The above example will output:

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

提交回复
热议问题