Describes in detail how php uses the exec () function under PDO to query the number of affected rows after execution.

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.