Store string with quotes in MySQL using PHP new version


/* Database connect using mysqli */

$dbHost = ‘localhost’;
$dbUsername = ‘root’;
$dbPassword = ”;
$dbName = ‘test’;
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if ($db->connect_errno) {
printf(“Connect failed: %s\n”, $db->connect_error);

/* Solve quotes issue using “addslashes” function in PHP. */ 

$str = “Is your name O’Reilly?”;

// Outputs: Is your name O\’Reilly?

$str = addslashes($str);

$query = “INSERT INTO table VALUES (‘$str’)”;
$sql = $db->query($query);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s