php-mysqlHow to escape JSON in PHP and MySQL?
JSON (JavaScript Object Notation) is a lightweight data-interchange format used for exchanging data between a server and a client. In PHP and MySQL, escaping JSON is done by using the json_encode() function. This function takes a PHP value and returns a JSON string representation of it.
$data = array("name" => "John", "age" => 30);
$json_data = json_encode($data);
echo $json_data;
Output example
{"name":"John","age":30}
The json_encode() function takes the following parameters:
$value: The value to be encoded.$options: Bitmask consisting ofJSON_HEX_QUOT,JSON_HEX_TAG,JSON_HEX_AMP,JSON_HEX_APOS,JSON_NUMERIC_CHECK,JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES,JSON_FORCE_OBJECT,JSON_PRESERVE_ZERO_FRACTION,JSON_UNESCAPED_UNICODE,JSON_PARTIAL_OUTPUT_ON_ERROR.
The json_encode() function returns a JSON encoded string on success and FALSE on failure.
Helpful links
More of Php Mysql
- How to check if a record exists in PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to use a MySQL union in PHP?
- How to use a variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to set a timeout for MySQL query in PHP?
- How to join tables with PHP and MySQL?
- How to get a key-value array from a MySQL database using PHP?
- How to change database in MySQL with PHP?
See more codes...