ThinkPHP framework to execute native SQL statements, thinkphpsql

Source: Internet
Author: User

ThinkPHP framework to execute native SQL statements, thinkphpsql

This article describes how to execute native SQL statements in the thinkPHP framework. We will share this with you for your reference. The details are as follows:

How to execute native SQL statements in thinkphp?

$ Model = new Model (); // or $ Model = D (); or $ Model = M (); $ SQL = "select * from 'order '"; $ voList = $ Model-> query ($ SQL );

Only an empty new Model is required to inherit the method in the Model.

Note:Query is the query function, while execute is the addition, deletion, and modification function.

Instance for querying and reading attribute values:

$sql = "select * from goods";$Model = M();$result = $Model->query($sql);foreach ($result as $k=>$val){$goods_id = $val["goods_id"];}

The tP model supports native SQL operations and provides two methods: query and execute. Why do native SQL have to distinguish between two methods? There are two reasons:

1. Different return types

Query is used for query. The returned dataset is the same as select or findall. Therefore, you can use the volist label in the template to output the query results.

Execute is used for write operations. The returned status or number of affected records

2. read/write statistics

To facilitate statistics on the number of reads and writes to the current data, separate the read and write operations of the database (corresponding to query and execute)

It's easy to use native SQL. We don't even need to instantiate any model, for example:

$ Model = new Model (); // instantiate an empty Model

The following method is equivalent

$ Model = D (); // or $ Model = M (); // execute the native SQL operation $ Model-> query ('select * from think_user where status = 1') below '); $ Model-> execute ('Update think_user set status = 1 where id = 1 ');

If you instantiate a model, you can still perform native SQL operations without being affected. For example:

$User = D('User');$User->query('select * from think_user where status=1');$User->execute('update think_user set status=1 where id=1');

In this case, we can simplify the writing of SQL statements, for example:

$User->query('select * from __TABLE__ where status=1');$User->execute('update __TABLE__ set status=1 where id=1');

The system automatically replaces _ TABLE _ with the name of the data TABLE corresponding to the current model. The actual data TABLE is determined by the model.

Generally, we use native SQL operations to implement some operations that are relatively difficult to implement by ORM and CURD. In addition, if SQL is not complex, the efficiency of native SQL is slightly different from that of coherent operations, AND THE ORM Implementation of TP itself is quite efficient.

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.