This article mainly introduces how php uses the exec () function under PDO to query the number of affected rows after execution, and analyzes the exec () When php uses pdo for addition, deletion, and modification () implementation tips and precautions for the number of affected rows after the function query operation is executed. For more information, see the following article () the method of affecting the number of rows after the function query is executed. combined with the instance form, the exec () implementation tips and precautions for the number of affected rows after the function query operation is executed. For more information, see
This example describes how php uses the exec () function under PDO to query the number of affected rows after execution. We will share this with you for your reference. The details are as follows:
exec()
MethodReturns the number of affected rows after execution..
Syntax:int PDO::exec(string statement)
Tip:
The statement parameter is the SQL statement to be executed. This method returns the number of affected rows during query execution, which is usually used in insert, delete, and update statements. However, it cannot be used for select queries and returns query results.
To verify this prompt, we 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();}
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();}
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();}
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;$i
getMessage();}
Note: In the preceding four query modes, only select queries cannot be executed normally..
The above section details how to use the exec () function in php to query the affected rows after execution. For more information, see other related articles in the first PHP community!