In PDO, we can execute SQL statements in three ways: exec (), query, and prepare () and execute ~
Three methods for executing SQL statements in PDO
In PDO, we can execute SQL statements in three ways: exec (), query, and prepare () and execute ~
In the previous article "using PDO constructor to connect databases and DSN details", we introduced how to use constructors to connect databases and DSN details, in this article, we will introduce three ways to execute SQL statements in PDO!
Method 1: exec ()
The exec () method returns the number of affected rows after the SQL statement is executed. the syntax format is as follows:
int PDO::exec(string statement)
The satatement parameter is the SQL statement to be executed. this method returns the number of affected rows during SQL statement execution, which is usually used in INSERT, DELETE, and UPDATE statements. The code is as follows:
Exec ($ query); // execute the add statement and return the affected rows echo "data added successfully, the affected rows are :". $ res;} catch (Exception $ e) {die ("Error! : ". $ E-> getMessage ().'
') ;}?>
Output result:
Method 2: query ()
The query () method is used to return the result set after the query is executed. the syntax format of this function is as follows:
PDOStatement PDO::query(string statement)
The satatement parameter is the SQL statement to be executed. It returns a PODStatement object! For details, see the following sample code:
Query ($ query); print_r ($ res);} catch (Exception $ e) {die ("Error! : ". $ E-> getMessage ().'
') ;}?>
Output result:
Note:
1. both query and exec can execute all SQL statements, but the return values are different.
2. query can implement all exec functions.
3. when the select statement is applied to exec, 0 is always returned.
4. if you want to view the specific query results, you can use the foreach statement to complete loop output.
Methods 3: pre-processing statement: prepare () statement and execute () statement
Pre-processing statements include prepare () and execute () methods. First, use the prepare () method to prepare for the query, then execute the query using the execute () method, and bindParam () method to bind the parameter to the execute () method, syntax:
PDOStatement PDO::prepare(string statement[,array driver_options])bool PDOStatement::execute([array input_parameters])
In PDO, the SQL query statement is executed through the prepare () and execute () pre-processing statements, and the data is recycled in a loop. The following is the sample code:
Prepare ($ query); // prepare the query statement $ res-> execute (); while ($ result = $ res-> fetch (PDO: FETCH_ASSOC )) {echo $ result ['id']. "". $ result ['username']. "". $ result ['password'].'
';}} Catch (Exception $ e) {die ("Error! : ". $ E-> getMessage ().'
');}
The output result is as follows:
We will introduce the three methods for executing SQL in PDO. is it very simple? you can test it locally, in the next article, we will continue to introduce how to obtain the result set in PDO. For more information, see the detailed description of the fetch () method for obtaining the result set in PDO.
The above is the details of the three methods for executing SQL statements in PDO. For more information, see other related articles in the first PHP community!