Luke Wenke
Luke Wenke

Reputation: 1138

Double quotes being removed from JSON data when saved via MySQLI

I'm using PHP 5.2 and when I use json_encode the double quotes are escaped (there's a slash behind them). When I try saving it using mysqli_query($db, "INSERT ...") it removes the slashes and when I try to retrieve the JSON object from the table it is invalid due to the double quotes in the strings not being escaped. I've currently got it set to utf8_unicode_ci. I had been using a sqlite2 database and had no problems.

mysqli_query($db, "INSERT INTO the_table (data) VALUES ('$json_data')");

an example of the data = 'video "Video Name".'; So it uses double quotes within a single quote string.

Upvotes: 0

Views: 305

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

You need to escape $json_data for inserting.

$stmt = mysqli_prepare($db, "INSERT INTO the_table (data) VALUES (?)");
mysqli_stmt_bind_param($stmt, 's', $json_data);
mysqli_stmt_execute($stmt);

Upvotes: 1

Related Questions