laravel 执行原生sql语句

864次阅读
没有评论

参考:Link

$db = DB::reconnect(‘MySQL1’);
$sql = ‘insert into a values (1);insert into b values(2);’;
$db->getPdo()->exec($sql);
$results = DB::select(‘select * from users where id = :id’, [‘id’ => 1]);
DB::insert(‘insert into users (id, name) values (?, ?)’, [1, ‘Dayle’]);
$deleted = DB::delete(‘delete from users’);
DB::statement(‘drop table users’);
DB::transaction(function () {
DB::table(‘users’)->update([‘votes’ => 1]);

DB::table('posts')->delete();

});
经测试:

DB::connection()->getPdo()->exec(“DELETE FROM command where id = ?”, [$id]);//使用占位符 错

DB::connection()->getPdo()->exec(“DELETE FROM command where id = $id”);//对

DB::delete(“DELETE FROM command where id = ?”, [$id]); //使用占位符 对 推荐

DB::delete(“DELETE FROM command where id = $id”); //对

DB::delete(“DELETE FROM command where id = :id”, [“id”=>$id]); //使用命名绑定 对 推荐

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 
评论(没有评论)