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

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

黄舟
黄舟Original
2017-03-29 09:07:411789browse

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();
}

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

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!

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