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; INSERTtry{ $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(); }
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(); }
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(); }
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<count($query);$i++){ print_r($query); } echo "select record success"; }catch(PDOException $e){ echo "Error".$e->getMessage(); }
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!