PHP Tutorial - 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

ParameterIs RequiredDescription
connectionRequired.MySQL connection
escapestringRequired.The not escaped string.

Return

It returns the escaped string.

Example

Escape special characters in a string for an SQL statement


<?php//w w w. jav  a  2  s  .c  o m
$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);
?>