怎样在thinkphp里面执行原生的sql语句

如题所述

原生SQL查询有 query() 和 execute() 两个方法:

query():用于 SQL 查询操作,并返回符合查询条件的数据集

execute():更新和写入数据的 SQL 操作,返回影响的记录数

public function read(){
    // 实例化一个空模型,没有对应任何数据表
    $Dao = M();
    //或者使用 $Dao = new Model();

    $list = $Dao->query("select * from user where uid<5");
    if($list){
        $this->assign('list', $list );
        $this->display();
    } else {
        $this->error($Dao->getError());
    }
}

public function read(){
    header("Content-Type:text/html; charset=utf-8");
    // 实例化一个空模型,没有对应任何数据表
    $Dao = M();
    //或者使用 $Dao = new Model();

    $num = $Dao->execute("update user set email = '[email protected]' where uid=3");
    if($num){
        echo '更新 ',$num,' 条记录。';
    }else{
        echo '无记录更新';
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-13
$array=$model - > query ("select * from user");
$bo=$model - > excute ("delete from user where userid = 1 limit 1");
相似回答