Execute SQL statement in PDO

Source: Internet
Author: User
Tags dsn getmessage prepare

exec () methodthe Exec () method returns the number of rows affected after execution, with the following syntax:int pdo::exec (string statement) 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 in Insert,delete and update statements. For example:


$dbms = ' mysql ';//database type $dbname= ' admin ';//Database used $user= ' root ';//database connection user name $pwd= ' password ';//database connection password $host= ' localhost ' ;//Database hostname $dsn= "$dbms: host= $host;p ort=3306;dbname= $dbName"; try{$pdo =new PDO ($DSN, $user, $pwd);//Initialize a PDO object, is to create a database connection object $pdo$query= "insert into User (Username,password) VALUES (' admin ', ' 123456 ') ';//SQL statement to execute $res= $pdo EXEC ($query);//Executes the Add statement and returns the number of affected rows echo "Data added successfully, the number of rows affected is:". $res; catch (Exception $e) {die ("error!:". $e->getmessage (). ' <br> ');}

The result of the operation is: data is added successfully, the number of rows affected is : 1

query () methodquery () method returns the result set after executing the query with the following syntax
pdostatement pdo::query (String statement) parameter statement is the SQL statement to execute. It returns a Pdostatement object
For example:
$dbms = ' MySQL '; $dbName = ' admin '; $user = ' root '; $pwd = ' 905407339 '; $host = ' localhost '; $dsn = "$dbms: host= $host;p ort=3306 ;d bname= $dbName "; try{$pdo =new PDO ($DSN, $user, $pwd); $query =" SELECT * from user ", $res = $pdo->query ($query);p Rint_r ($res);} catch (Exception $e) {die ("error!:". $e->getmessage (). ' <br> ');}

run Result: Pdostatement Object ([queryString] = select * from user)
If you want to see the exact results of a query, you can complete the loop output through a foreach statement such as:
foreach ($res as $val) {echo $val [' username ']. " ----". $val [' Password ']. ' <br> ';}

Operation Result: 107lab----e10adc3949ba59abbe56e057f20f883e
Admin----123456
Description: If you want to view the detailed usage of foreach, please see: http://blog.csdn.net/qq_28602957/article/details/50971524

Query () and exec ()
Query can implement all the functions of exec
For example:
$dbms = ' mysql ';//database type $dbname= ' admin ';//Database used $user= ' root ';//database connection user name $pwd= ' 905407339 ';//database connection password $host= ' localhost ';//database hostname $dsn= "$dbms: host= $host;p ort=3306;dbname= $dbName"; try{$pdo =new PDO ($DSN, $user, $pwd);// Initializing a PDO object is creating a database connection object $pdo$query= "insert into User (Username,password) VALUES (' admin ', ' 123456 ') ';//SQL statement to be executed $res = $pdo->query ($query);//Executes the Add statement and returns the number of rows affected echo "Data added successfully, the number of rows affected is:". $res->rowcount (); catch (Exception $e) {die ("error!:". $e->getmessage (). ' <br> ');}

The result of the operation is:data is added successfully, the number of rows affected is : 1

Note:

1. Both query and exec can execute all SQL statements, but the return value is different.

2. Query can realize all the functions of exec.

3. When the SELECT statement is applied to exec, it always returns 0



preprocessing statements----prepare () statements and execute () statementspreprocessing statements include both the prepare () and the execute () methods. First, the query preparation is done through the prepare () method, then the query is executed through the Execute () method, and the Bindparam () method can be used to bind parameters to the Execute () method, with the following syntax: pdostatement PDO::p repare (String Statement[,array driver_options])bool Pdostatement::execute ([array input_parameters])For example:Execute SQL query statements in PDO through preprocessing statements prepare () and execute (), and apply the while () statement and Fetch () method to complete the loop output of the data
$dbms = ' mysql ';//database type $dbname= ' admin ';//Database used $user= ' root ';//database connection user name $pwd= ' 905407339 ';//database connection password $host= ' localhost ';//database hostname $dsn= "$dbms: host= $host;p ort=3306;dbname= $dbName"; try{    $pdo =new PDO ($DSN, $user, $pwd);// Initializes a PDO object that creates a database connection object $pdo    $query = "SELECT * from User",//SQL statement to execute    $res = $pdo->prepare ($query);// Prepare the query statement    $res->execute ();    while ($result = $res->fetch (PDO::FETCH_ASSOC)) {        echo $result [' ID ']. " ". $result [' username ']." ". $result [' Password ']. ' <br> ';    }} catch (Exception $e) {die    ("error!:". $e->getmessage (). ' <br> ');}

The result of the operation is:1 107lab e10adc3949ba59abbe56e057f20f883e
4 Admin 123456
5 Admin 123456


The way to get a result set in PDO is in the authoring phase, so please look forward to it.

Execute SQL statement in PDO

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.