laravel执行原生sql
发布时间:2021-06-16, 18:20:20 分类:PHP | 编辑 off 网址 | 辅助
正文 742字数 88,981阅读
1.插入数据
DB::insert('insert into test (id, name, email, password) values (?, ?, ? , ? )',[1, 'Laravel','laravel@test.com','Laravel']);
Run code
Cut to clipboard
2.查询语句
$user = DB::select('select * from test where id = ?', [1]);
//我们还 可以看到在执行查询的时候使用了参数绑定,以避免SQL注入。除此之//外还可以使用命名绑定:
$user = DB::select('select * from test where id = :id', [':id'=>1]);
Run code
Cut to clipboard
3.更新语句
$resule = DB::update('update test set name="laraveltest" where name = ?', ['laravel']);
//此处返回值为影响的行数请注意 不能直接做判断的
if($resule || $resule===0){
//成功的提示语
return true;
}else{
//失败的提示语
return false;
}
Run code
Cut to clipboard
4.删除语句
$deleted = DB::delete('delete from test');
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
暂无评论 »