Native query
Db
class supports native SQL
query operations, mainly including the following two methods:
query
Method
query
method is used to perform SQL
a query operation and returns False if the data is illegal or a query error, otherwise the query result dataset (the same select
method) is returned.
Examples of Use:
Db::query("select * from Think_user where Status=1");
If you are currently using a distributed database and have read-write separation, the query method is always executed at the read server, so the query method corresponds to a read operation, regardless of your SQL statement.
execute
Method
Execute SQL operation used to update and write data, or False if the data is illegal or query error, otherwise the number of records affected is returned.
Examples of Use:
Db::Execute("Update think_user set name= ' thinkphp ' where Status=1");
If you are currently using a distributed database, and you have set up read-write separation, the Execute method is always executed on the write server, so the Execute method corresponds to a write operation, regardless of your SQL statement.
Parameter binding
Supports the use of parameter bindings at native queries, including question-mark placeholders or named placeholders, such as:
Db::query ("SELECT * fromThink_userwhere ID=? and Status=?", [8,1]);//Name Binding Db::execute ("UpdateThink_userSet name=:name where Status=:Status", [' name ' = ' thinkphp ', ' status ' =>1]);
Database-Basic native query