Home  >  Article  >  Backend Development  >  PHP uses the exec() function under PDO to implement the method of querying the number of affected rows after execution

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

墨辰丷
墨辰丷Original
2018-05-24 14:21:242085browse

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','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;$igetMessage();
}

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!

Statement:
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