Laravel 不能使用 mysql_real_escape_string()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12623121/
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
Laravel cannot use mysql_real_escape_string()
提问by Nyxynyx
I get a database connection error when trying to use mysql_real_escape_string()within Laravel. Fluent queries work properly so I assume that the database has been configured correctly.
尝试mysql_real_escape_string()在 Laravel 中使用时出现数据库连接错误。Fluent 查询工作正常,因此我假设数据库已正确配置。
How should mysql_real_escape_string()be used from Laravel? I'm using it to escape the values in a SQL query that I need to build myself due to limitations of Fluent.
应该如何mysql_real_escape_string()从 Laravel 使用?由于 Fluent 的限制,我使用它来转义我需要自己构建的 SQL 查询中的值。
PHP Code that builds my own SQL query
构建我自己的 SQL 查询的 PHP 代码
foreach($listings as $listing) {
$listing = get_object_vars($listing);
$query = 'INSERT IGNORE into archive ';
$query .= '(' . implode(',', array_keys($listing)) . ') ';
$query .= 'VALUES(' . implode(',', array_values( array_map('mysql_real_escape_string', $listing) )) . ')';
DB::query($query);
}
Error
错误
mysql_real_escape_string() [function.mysql-real-escape-string]:
Access denied for user 'nobody'@'localhost' (using password: NO)
采纳答案by Sherlock
Laravel uses PDO, so there's no escaping, just prepared statements. See the Laravel manual on databases.
Laravel 使用PDO,所以没有转义,只是准备好的语句。请参阅有关数据库的Laravel 手册。
回答by AMIB
use DB::connection()->getPdo()->quote()instead.
使用DB::connection()->getPdo()->quote()来代替。
回答by rael_kid
mysql_real_escapes_string()uses a database link created with mysql_connect(), so it can only be used after you've called mysql_connect().
mysql_real_escapes_string()使用由 建立的数据库链接mysql_connect(),因此它只能在您调用 后使用mysql_connect()。
An important note about this (from the comments):
关于这一点的重要说明(来自评论):
.. But shouldn't be used in environments where PDO is the database driver of choice. In fact, mysql_connect() shouldn't be used at all anymore. – Robin v. G.
.. 但不应在 PDO 是首选数据库驱动程序的环境中使用。实际上,根本不应再使用 mysql_connect()。– 罗宾诉 G.
回答by Dirk
My solution for this:
我对此的解决方案:
- Create custom helpers file in app/lib/helpers.php
Add this to autoload in composer.json:
"files": [ "app/lib/helpers.php" ],Add this function (found on php.net)
if ( !function_exists('mysql_escape')) { function mysql_escape($inp) { if(is_array($inp)) return array_map(__METHOD__, $inp); if(!empty($inp) && is_string($inp)) { return str_replace(array('\', "
", "\n", "\r", "'", '"', "\x1a"), array('\\', '\0', '\n', '\r', "\'", '\"', '\Z'), $inp); } return $inp; } }"files": [ "app/lib/helpers.php" ],php artisan dump-autoload
- 在 app/lib/helpers.php 中创建自定义助手文件
将此添加到 composer.json 中的自动加载:
if ( !function_exists('mysql_escape')) { function mysql_escape($inp) { if(is_array($inp)) return array_map(__METHOD__, $inp); if(!empty($inp) && is_string($inp)) { return str_replace(array('\', "##代码##", "\n", "\r", "'", '"', "\x1a"), array('\\', '\0', '\n', '\r', "\'", '\"', '\Z'), $inp); } return $inp; } }添加此功能(在 php.net 上找到)
##代码##php artisan dump-autoload
Now you can use mysql_escape everywhere in your code.
现在您可以在代码中的任何地方使用 mysql_escape。

