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 variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to change database in MySQL with PHP?
- How to use MySQL transactions in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to replace a string in MySQL using PHP?
- How to get a single value from query in PHP MySQL?
- How to get table column names in PHP MySQL?
- How to get the first row of a result in MySQL using PHP?
See more codes...