thinkphp 怎么样执行Mongo原生语句

2025-04-29 01:26:44
推荐回答(2个)
回答1:

原生SQL查询有 query() 和 execute() 两个方法:
query():用于 SQL 查询操作,并返回符合查询条件的数据集
execute():更新和写入数据的 SQL 操作,返回影响的记录数

1
2
3
4
5
6
7
8
9
10
11
12
13

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 = '12345@xxx.com' where uid=3");
if($num){
echo '更新 ',$num,' 条记录。';
}else{
echo '无记录更新';
}
}

回答2:

我建议你看下 开发文档的数据库选项 里面有个 原生SQL语句