Home Submit a Ticket Knowledgebase News
Or login with:
Knowledgebase : MySQLi
Fungsi mysqli_real_escape_string()
Posted by Ari Sigit on 10 June 2019 01:12 pm

Definisi dan Penggunaan

Fungsi mysqli_real_escape_string () lolos dari karakter khusus dalam string untuk digunakan dalam pernyataan SQL.

Contoh Script:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$age = mysqli_real_escape_string($con, $_POST['age']);

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('$firstname', '$lastname', '$age')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

Butuh Bantuan? Hubungi kami: support@buyssl.id