初學ThinkPHP的時候,很多人都對execute()和query()方法的區(qū)別搞不懂,本文就此淺析二者的區(qū)別。
大家都知道,ThinkPHP中execute()和query()方法都可以在參數(shù)里直接輸入SQL語句。但是不同的是execute()通常用來執(zhí)行insert或update等SQL語句,而query常用來執(zhí)行select等語句。
execute()方法將返回影響的記錄數(shù),如果執(zhí)行SQL的select語句的話,返回的結果將是表的總記錄數(shù):
復制代碼 代碼如下:
$model = M( "MyTable" );
$result = $model ->execute( 'update MyTable set name=aaa where id=11'); //將返回總行數(shù)
query()方法將返回數(shù)據(jù)集:
復制代碼 代碼如下:
$model = M( "MyTable" );
$result = $model ->query( 'select * from MyTable' ); //將返回array()