Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between execute () and query () in ThinkPHP

Shulou Source: shulou.com Published: 2022-06-02 10:40:35 10月18日 Update

This article shows you the difference between execute () and query () in ThinkPHP. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Both the execute () and query () methods in ThinkPHP can enter SQL statements directly in the parameters.

The difference is that execute () is usually used to execute SQL statements such as insert or update, while query is often used to execute statements such as select.

The execute () method returns the number of records affected, and if the select statement of SQL is executed, the result will be the total number of records in the table:

$model = M ("MyTable")

$result = $model-> execute ('select * from MyTable'); / / the total number of rows will be returned

The total number of rows returned by execute is similar to that in native php.

$sql = 'select `status` from `sys_ serinfo` where `status` = 1'

$query = mysql_query ($sql)

$result = mysql_num_rows ($query)

/ / mysql_num_rows from manual returns the number of rows in the result set. This command is valid only for SELECT statements.

Echo $result

The query () method returns the dataset:

$model = M ("MyTable")

$result = $model-> query ('select * from MyTable'); / / array () will be returned

Execute () is mainly used for updates and writes, and execute () is used for the number of affected rows but no result set; there is no need to return the pending value from the database.

Query () is mainly used to query the number of affected rows and result sets to get the data that needs to be processed from the database.

-

What is the difference between execute () and query () in ThinkPHP? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Result statement data method influence content head line skill database knowledge processing different effective concise concise parameter command that is number article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn Shulou Information Shulou Technology Microsoft