网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   PHP (https://www.0058.net/forumdisplay.php?f=75)
-   -   浅析ThinkPHP中execute和query方法的区别 (https://www.0058.net/showthread.php?t=5556)

Wabasha 2016-05-12 10:26 PM

浅析ThinkPHP中execute和query方法的区别
 
初学ThinkPHP的时候,很多人都对execute()和query()方法的区别搞不懂,本文就此浅析二者的区别。
大家都知道,ThinkPHP中execute()和query()方法都可以在参数里直接输入SQL语句。但是不同的是execute()通常用来执行insert或update等SQL语句,而query常用来执行select等语句。
execute()方法将返回影响的记录数,如果执行SQL的select语句的话,返回的结果将是表的总记录数:
Code:

$model = M( "MyTable" );
$result = $model ->execute( 'update MyTable set name=aaa where id=11'); //将返回总行数

query()方法将返回数据集:
Code:

$model = M( "MyTable" );
$result = $model ->query( 'select * from  MyTable' ); //将返回array()



All times are GMT +8. The time now is 09:20 PM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.