How to obtain the number of rows affected by additions, deletions and modifications through SQL aggregate functions?

黄舟
Release: 2017-05-20 17:13:48
Original
1849 people have browsed it

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:

查询关键字:
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT COUNT(*) FROM user WHERE username = :id";//需要执行的sql语句 $stmt = $dbh->prepare($sql);//准备查询语句 $stmt->bindParam(':id', $_POST['nr'], PDO::PARAM_STR); //执行查询语句,并返回结果集 $result = $stmt->execute(); echo 'There are',$stmt->fetchColumn(),'rows returned.'; }catch (PDOException $e){ echo 'PDO Exception Caught.'; echo 'Error with the database:
'; echo 'SQL Query:',$sql; echo 'Error:'.$e->getMessage(); } } ?>
Copy after login

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:

How to obtain the number of rows affected by additions, deletions and modifications through SQL aggregate functions?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!