I think the question hides a few misconceptions so I'll elaborate a proper answer.
First of all, mysql_real_escape_string() is a function from the legacy mysql extension. As such:
- It's no longer maintained
- It'll trigger E_DEPRECATED warnings in PHP/5.5
- It'll no longer be available in future PHP releases
And I'm not talking about the function, I'm talking about the entire extension.
Additionally, you cannot use it if you are not using the deprecated legacy mysql extension. If you use PDO, MySQLi, ADODB or anything else, it's useless and it won't work. Needless to say, it won't work either if you are using SQLite, Oracle, SQL Server or PostgreSQL. All DB extensions have (or should have) an alternative tool.
Now, the Joomla framework provides its own database classes. You appear to be using version 2.5 and the escape function is JDatabase::quote()
. That's how the feature works in Joomla. I don't really understand why you think it might be unreliable but, if you think so, you'd better drop the complete JDatabase
and use something else. What you cannot do is to mix stuff from different extensions that aren't designed to work together.
Edit: I've grabbed Joomla 2.5 and had a look at the source code. The quote()
function is a wrapper for escape()
, which belongs to an abstract class, JDatabase
, that implements an interface, JDatabaseInterface
. There are three implementations:
JDatabaseMySQL
/**
* Method to escape a string for usage in an SQL statement.
*
* @param string $text The string to be escaped.
* @param boolean $extra Optional parameter to provide extra escaping.
*
* @return string The escaped string.
*
* @since 11.1
*/
public function escape($text, $extra = false)
{
$result = mysql_real_escape_string($text, $this->getConnection());
if ($extra)
{
$result = addcslashes($result, '%_');
}
return $result;
}
JDatabaseMySQLi
/**
* Method to escape a string for usage in an SQL statement.
*
* @param string $text The string to be escaped.
* @param boolean $extra Optional parameter to provide extra escaping.
*
* @return string The escaped string.
*
* @since 11.1
*/
public function escape($text, $extra = false)
{
$result = mysqli_real_escape_string($this->getConnection(), $text);
if ($extra)
{
$result = addcslashes($result, '%_');
}
return $result;
}
JDatabaseSQLSrv
/**
* Method to escape a string for usage in an SQL statement.
*
* The escaping for MSSQL isn't handled in the driver though that would be nice. Because of this we need
* to handle the escaping ourselves.
*
* @param string $text The string to be escaped.
* @param boolean $extra Optional parameter to provide extra escaping.
*
* @return string The escaped string.
*
* @since 11.1
*/
public function escape($text, $extra = false)
{
$result = addslashes($text);
$result = str_replace("\'", "''", $result);
$result = str_replace('\"', '"', $result);
$result = str_replace('\\\/', '/', $result);
$result = str_replace('\\\\', '\\', $result);
if ($extra)
{
// We need the below str_replace since the search in sql server doesn't recognize _ character.
$result = str_replace('_', '[_]', $result);
}
return $result;
}
So, is quote()
the same as mysql_real_escape_string()
? Obviously not. Does it do the same? Yes.