Detailed introduction to how PHP uses the exec() function under PDO to query the number of affected rows after execution.

黄舟
Release: 2023-03-07 07:02:02
Original
1715 people have browsed it

This article mainly introduces phpUsing PDO under exec()FunctionHow to query the number of affected rows after execution , combined with the example form, it analyzes the related implementation skills and precautions of the exec() function querying the number of affected rows after the operation is executed when using PDO to perform add, delete, and modify operations. Friends in need can refer to the following

The example of this article describes how PHP uses the exec() function under PDO to query the number of affected rows after execution. Share it with everyone for your reference, the details are as follows:

exec()MethodReturns the number of affected rows after execution.

Syntax: int PDO::exec(string statement)

##Tips:

Parameters statement is the SQL statement to be executed.

This method returns the number of rows affected when executing the query, usually used in insert, delete and update statements. But it cannot be used for select query, and the query result is returned.

In order to verify this prompt, I will test the insert, delete, update, and select queries respectively;

INSERT

try{
 $conn=new PDO("mysql:host=$servername;dbname=$dbname", $username,$password);
 $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
 $sql="INSERT INTO `hello`(`firstname`,`lastname`,`email`)values('ye','xianming','1150416034@qq.com'),
  ('xiao','hua','xiaohua@163.com')";
 $conn->exec($sql);
 echo "Insert record success";
}catch(PDOException $e){
  echo "Error:".$e->getMessage();
}
Copy after login

Delete

try{
 $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
 $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
 $sql="delete from hello where id=61";
 $conn->exec($sql);
 echo "delete record success";
}catch(PDOException $e){
  echo "Error".$e->getMessage();
}
Copy after login

Update

try{
 $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
 $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
 $sql="UPDATE hello SET firstname='xiao',lastname='ming' WHERE id='62'";
 $conn->exec($sql);
 echo "update record success";
}catch(PDOException $e){
 echo "Error".$e->getMessage();
}
Copy after login

Select

try{
 $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
 $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
 $sql="select * from hello";
 $query=$conn->exec($sql);
 for($i=0;$igetMessage();
}
Copy after login

Note: Of the above four query methods, only the select query cannot be executed normally.

The above is the detailed content of Detailed introduction to how PHP uses the exec() function under PDO to query the number of affected rows after execution.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
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!