Mysql 查询生成器不显示变量?
我正在运行以下代码:Mysql 查询生成器不显示变量?,mysql,laravel,query-builder,Mysql,Laravel,Query Builder,我正在运行以下代码: $id = 1; $email = 'email@gmail.com'; $user = DB::table('users')->where([ ['id', '=', $id], ['email', '=', $email] ])->toSql(); dd($user); 但查询生成器打印是: select * from `users` where (`id` = ? and `email` = ?) 为什么不打印是: select * from
$id = 1;
$email = 'email@gmail.com';
$user = DB::table('users')->where([
['id', '=', $id],
['email', '=', $email]
])->toSql();
dd($user);
但查询生成器打印是:
select * from `users` where (`id` = ? and `email` = ?)
为什么不打印是:
select * from `users` where (`id` = 1 and `email` = email@gmail.com)
这就是toSql()
方法的工作原理。它只显示您准备好的查询,但不执行它
要执行查询,请使用或类似的方法:
$user = DB::table('users')->where([
['id', '=', $id],
['email', '=', $email]
])->first();
查询生成器插入字符代替值以保护您免受sql注入的影响,然后他会根据需要为您设置值,您将得到最终结果,您在屏幕上显示的事实只是查看查询,查询生成器插入字符来代替值,以保护您免受SQL注入的影响。我相信@БМцццц-Сцццц回答您的查询很好 您可以使用以下代码减少代码行,而不是直接使用原始SQL使用模型:
$id = 1;
$email = 'email@gmail.com';
$user = User::where('id',$id)
->where('email',$email)
->get();
dd($user);
我知道方法
get()
,find()
,first()
,但我的问题是查询生成器不打印select*from`users`where(`id`=1,`email`=email@gmail.com)
而不是从'users'中选择*,其中('id`=?和'email`=?)
谢谢:拇指:,你有打印值的方法吗?它不需要在屏幕上打印给你,这是一种特殊的安全机制,在我看来,你不需要在代码中看到这一点,它足以在你的头脑中保留一些点,但可以单独显示变量,例如,在一个数组中。Tank you:thumb: