I have some information that is sent from my client to my server in the following format:
{
identifier: {
value: null,
public: false
}
}
Obviously the value isn't always null, but there are times when it needs to be, when sending this over to PHP it saves "null" as a String during the json_decode
phase. When trying to insert null values into the SQL database, instead of NULL
I get "null"
which is causing strange things to happen on my client. (IE: Displaying "null" instead of nothing).
Obviously I could just do a check on the client for "null" but I see no reason to continuously spend the additional bytes of data to send the text "null" to the client.
EXAMPLE:
$identifier = $json_array['identifier'];
$statement = $connection->prepare("UPDATE table SET v1 = :val, p1 = :pub");
$statement->bindParam(":val", $identifier['value']);
$statement->bindParam(":pub", $identifier['public']);
$statement->execute();
Where v1 is a VARCHAR value
ALWAYS verify your ideas.
it saves "null" as a String during the json_decode phase.
Wrong.
$json = '{
"identifier": {
"value": null,
"public": false
}
}';
$obj = json_decode($json);
var_dump($obj);
clearly says value is null.
Which, in turn, gets inserted all right in MySQL using an PHP array and PDO prepared statement.