PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_real_escape_string() function in PHP


Tutorials »Php »

Topic

What is mysql_real_escape_string() function in PHP?
How does mysql_real_escape_string() works?




Explanation

This mysql_real_escape_string() function in php escapes special characters in a string for use in an SQL statement.

Syntax

string mysql_real_escape_string(string unescaped_string [,resource link_identifier])

Returns the escaped string on success, or FALSE on failure.

This function will escape special characters in the unescaped_string, this differs from mysql_escape_string() by taking into account of connection's current charset, so that it is safe to place it in a mysql_query(). This function does not escape % and _.

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.

Example


<?php
//Attempt to connect to the default database server
$conn = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

$item = "Zak's and Derick's Laptop";

//escape special character in the string
$escaped_item = mysql_real_escape_string($item, $conn);

//print escaped string
printf("Escaped string: %s\n", $escaped_item);

?>



In the above code, single quotes is escaped by back slash , now the string is an escaped string.
RESULT:

Escaped string: Zak\'s and Derick\'s Laptop


See also: mysql_escape_string() and mysql_client_encoding().





Other Links

web hosting