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 use a MySQL union in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to join tables with PHP and MySQL?
- How to get a single value from query in PHP MySQL?
- How to insert a null value in MySQL using PHP?
- How to use the LIKE operator in PHP and MySQL?
- How to check the result of an insert in PHP and MySQL?
- How to check if a record exists in PHP and MySQL?
- How to list databases in PHP and MySQL?
- How to get table column names in PHP MySQL?
See more codes...