Database-Basic native query

Source: Internet
Author: User

Native query

Dbclass supports native SQL query operations, mainly including the following two methods:

queryMethod

querymethod 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.

executeMethod

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

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.