Re: PDO native JSON / array support
| From: | Rowan Collins | Date: | Wed, 26 Jul 2017 20:09:40 +0000 |
| Subject: | Re: PDO native JSON / array support | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100053@lists.php.net to get a copy of this message | ||
On 26 July 2017 14:02:03 BST, "Michał" <aaatoja@o2.pl> wrote:
>JSON should be automatically converted to proper type(s).
>
>$data = ['name' => 'John'];
>$sth = $dbh->prepare('INSERT INTO table (json_column) values (:data)');
>$sth->bindParam(':data', $data, PDO::PARAM_JSON); // new parameter
>
>
>SELECT json_column FROM table;
>$stmt->fetch(PDO::FETCH_ASSOC) should return
>
>array(
> [json_column] => array('name' => 'John')
>)
This feels like unnecessary magic to me, when you can already write this, and have all the power and
flexibility of the existing JSON functions:
$data = ['name' => 'John'];
$sth = $dbh->prepare('INSERT INTO table (json_column) values (:data)');
$sth->bindParam(':data', json_encode($data), PDO::PARAM_STRING);
As I say, array types are much more fiddly, so support for those would be great - that could just be
well-tested encode /decode functions.
Regards,
--
Rowan Collins
[IMSoP]