Details how PHP uses the PDO exec () function to query the number of rows affected after execution

Source: Internet
Author: User
This article mainly introduces PHP using the PDO under the EXEC () function query after the execution of the affected rows of the method, combined with an instance of PHP in the use of PDO for the operation when the EXEC () function query operation after the execution of the affected line number of the relevant implementation tips and considerations, the need for friends can refer to the next

This example describes how PHP uses the EXEC () function in PDO to query the number of rows affected after execution. Share to everyone for your reference, as follows:

exec()Method returns the number of rows affected after execution .

Grammar:int PDO::exec(string statement)

Tips:

The parameter statement is the SQL statement to execute. The method returns the number of rows that were affected when the query was executed, typically used in insert,delete and UPDATE statements. However, you cannot use the Select query to return the query results .

To verify this hint, I test the insert,delete,update,select query separately;

INSERT

try{$conn =new PDO ("mysql:host= $servername;d bname= $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;d bname= $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;d bname= $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;d bname= $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: The above four query methods, and finally only select query does not execute normally .

Related Article

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.