GregHBushnell GregHBushnell - 3 months ago 25
HTML Question

php mysql_real_escape_string conversion to mysqli

Hi im currently learning php and mysql a tutorial i am following uses the now out of date mysql_real_escape_string ive read the manual on mysqli's version of it but just cant really figure out how i would do something like the code below.

$username = mysql_real_escape_string($POST['username']);


any help in converting this over to mysqli and any tips are greatly apreciated

Answer

I think you want this

<?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
$username = mysqli_real_escape_string($con, $_POST['username']);
mysqli_close($con);
?>