How to obtain the number of affected rows by additions, deletions and modifications through SQL aggregate functions?
Being able to calculate the number of rows returned or affected by a query before taking further action is usually very useful. This function is especially convenient when you want to display results in pages or generate statistical information. So how is the function of getting the number of affected rows implemented?
1. Count the number of late result records through the SQL aggregation function COUNT
COUNT returns the number of rows of a query or part of a query, usually the same as DINSTINCT use together. SQL's aggregate function COUNT is widely supported by a variety of databases!
2. Return the number of affected rows through the PDOStatement->rowCount() method
Obtained by the PDOStatement->rowCount() method How many rows are affected by UPDATE, INSERT or DELECTE queries. The rowCount() method is not common in typical PHP applications, but it can count how many rows were affected after calling PDOStatement->execute.
3. Application Example
Use the SQL aggregate function COUNT to count the number of records returned by the query. The specific code is as follows:
Run this example, enter the name of the database to be queried in the text box, click the "Query" button, and the number of rows of the queried results will be displayed, as shown below:
Note:
When using UPDATE and DELETE statements, be sure to add a WHERE clause at any time. If the WHERE clause is not added, all columns in the database may be updated or deleted from the table. All the data is not what the user wants to see in any case.
The above is the detailed content of How to obtain the number of rows affected by additions, deletions and modifications through SQL aggregate functions?. For more information, please follow other related articles on the PHP Chinese website!