$item = "Zak's Laptop";
$a=mysql_connect('localhost','root','');
//$escaped_item = mysql_escape_string($item);
$escaped_item = mysql_real_escape_string($item);
echo $escaped_item;
?>
output : Zak\'s Laptop
See the code above we use the server connection, in case of using the mysql_real_escape_string,
if you are not mention the server connection
i.e $a=mysql_connect('localhost','root','');
you may get the error below
Warning: mysql_real_escape_string(): Access denied for user 'ODBC'@'localhost' (using password: NO) in c:\program files\easyphp1-8\www\currentworking\bikes_adda\mysql.php on line 5
Warning: mysql_real_escape_string(): A link to the server could not be established in c:\program files\easyphp1-8\www\currentworking\bikes_adda\mysql.php on line 5
so before using mysql_real_escape_string , use server connection.
BUT In case of mysql_escape_string , we noneed to use of server conncetion.
just use the code normally...
$item = "Zak's Laptop";
$escaped_item = mysql_escape_string($item);
echo $escaped_item;
?>
output : Zak\'s Laptop
Note: mysql_escape_string() does not escape % and _.
Showing posts with label mysql_real_escape_string. Show all posts
Showing posts with label mysql_real_escape_string. Show all posts
diff between mysql_escape_string and mysql_real_escape_string
Posted by
Danduanilkumar
on Monday, August 31, 2009
Labels:
mysql_escape_string,
mysql_real_escape_string
/
Comments: (0)
mysql_real_escape_string
Posted by
Danduanilkumar
on Sunday, August 30, 2009
Labels:
mysql,
mysql_real_escape_string,
php,
sql injection
/
Comments: (0)
mysql_real_escape_string
(PHP 4 >= 4.3.0, PHP 5)
mysql_real_escape_string -- Escapes special characters in a string for use in a SQL statement
mysql_real_escape_string -- Escapes special characters in a string for use in a SQL statement
Description
string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] )Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.
mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.
This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.
Parameters
- unescaped_string
- The string that is to be escaped.
- link_identifier
- The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.
Examples
Example 2. An example SQL Injection Attack
|
Example 3. A "Best Practice" query Using mysql_real_escape_string() around each variable prevents SQL Injection. This example demonstrates the "best practice" method for querying a database, independent of the Magic Quotes setting.
|
Notes
Note: A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. If link_identifier isn't defined, the last MySQL connection is used.
Note: If magic_quotes_gpc is enabled, first apply stripslashes() to the data. Using this function on data which has already been escaped will escape the data twice.
Note: If this function is not used to escape data, the query is vulnerable to SQL Injection Attacks.
Note: mysql_real_escape_string() does not escape % and _. These are wildcards in MySQL if combined with LIKE, GRANT, or REVOKE.