淺析thinkphp中execute和query方法的區(qū)別

字號(hào):


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