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 ();}
The above is the whole content of this article, I hope that everyone's study has helped.