PHP uses the exec() function under PDO to implement the method of querying the number of affected rows after execution

墨辰丷
Release: 2023-03-27 19:26:01
Original
2063 people have browsed it

This article mainly introduces how PHP uses the exec() function under PDO to query the number of affected rows after execution. Combined with an example, it analyzes the impact of the exec() function query after the execution of PDO when using pdo to perform add, delete, and modify operations. For related implementation skills and precautions that affect the number of rows, friends in need can refer to

This article describes the method of using PDO's exec() function 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','[email protected]'),
  ('xiao','hua','[email protected]')";
 $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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

How to troubleshoot that the exec() function in PHP has no return value

curl_execFunction introduction and detailed explanation of usage

php four functions shell_exec, exec, passthru, system respectively usage scenarios

The above is the detailed content of PHP uses the exec() function under PDO to implement the method of querying 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 [email protected]
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!