PHP mysqli_real_escape_string() Function
Definition
The mysqli_real_escape_string() function escapes special characters in a string for an SQL statement.
Syntax
PHP mysqli_real_escape_string() Function has the following syntax.
mysqli_real_escape_string(connection,escapestring);
Parameter
Parameter | Is Required | Description |
---|---|---|
connection | Required. | MySQL connection |
escapestring | Required. | The not escaped string. |
Return
It returns the escaped string.
Example
Escape special characters in a string for an SQL statement
<?php// w ww . j a v a 2s . c om
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$myName="Jr's";
$myName=mysqli_real_escape_string($con,$myName);
mysqli_query($con,"INSERT into emp (name) VALUES ('$myName')");
mysqli_close($con);
?>