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.
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.
PHP Code that builds my own SQL query
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)
Laravel uses PDO, so there’s no escaping, just prepared statements. See the Laravel manual on databases.