如何在 Laravel 5.2 中使用真正的转义字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36283141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use real escape string in Laravel 5.2
提问by Qazi
is there any built-in way or method available to implement mysqli_real_escape_string
method. I have some custom data on which I have to implement this method.
是否有任何内置方式或方法可用于实现mysqli_real_escape_string
方法。我有一些自定义数据,我必须在这些数据上实现此方法。
I tried this \DB::escape()
method in laravel but got following error.
我\DB::escape()
在 Laravel 中尝试过这种方法,但出现以下错误。
ErrorException in DatabaseManager.php line 296: call_user_func_array()
expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'escape'
DatabaseManager.php 第 296 行中的 ErrorException:call_user_func_array()
期望参数 1 是一个有效的回调,类 'Illuminate\Database\MySqlConnection' 没有方法 'escape'
while If I try manually implement this method mysqli_real_escape_string
got following error.
而如果我尝试手动实现此方法会mysqli_real_escape_string
出现以下错误。
ErrorException in Contact.php line 348: mysqli_real_escape_string()expects exactly 2 parameters, 1 given
Contact.php 第 348 行中的 ErrorException:mysqli_real_escape_string()需要 2 个参数,1 个给定
I did some R&D, for this method mysqli_real_escape_string()
I have to pass 2 parameter 1 is DB connection link, and 2nd is string to implement method action. but now, How I will know Laravel DB connection link?
我做了一些研发,对于这个方法mysqli_real_escape_string()
我必须传递2个参数1是DB连接链接,2nd是字符串来实现方法动作。但是现在,我如何知道 Laravel DB 连接链接?
any idea or solution for this ?
对此有什么想法或解决方案吗?
回答by GrumpyCrouton
Laravel uses PDO, so there's no escaping, just prepared statements. See the Laravel manual on databases.
Laravel 使用PDO,所以没有转义,只是准备好的语句。请参阅有关数据库的Laravel 手册。
If you absolutely need this type of feature, you could try DB::connection()->getPdo()->quote()
instead.
如果你绝对需要这种类型的功能,你可以试试DB::connection()->getPdo()->quote()
。