|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2007-09-05 18:22 UTC] prikid at gmail dot com
 Description:
------------
When using json_encode() and trying to insert into mysql table field with single quotes - single quote is not escaped and mysql produces error.
Reproduce code:
---------------
$name = "Mike O'Brien";
$email = "test@tes.com";
$settings = array("name"=> $name,"email" => $email);
$json = json_encode($settings);
$sql = "INSERT INTO `users` VALUES('".$json."')";
$result = mysql_query($sql) or die (mysql_error());
Expected result:
----------------
Successfull mysql insertion with escaped single quote.
Actual result:
--------------
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Brien
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 18:00:01 2025 UTC | 
$name = "Mike O'Brien"; $name = mysql_escape_string($name); $email = "test@test.com"; $settings = array("name" => $name,"email" => $email); $json = json_encode($settings); var_dump($json); --- output is: string(49) "{"name":"Mike O\\'Brien","email":"test@test.com"}" Double quoted and still generates error when trying to insert into mysql db.