9951 explained code solutions for 126 technologies


php-pdoGet JSON from table with PHP PDO


We have test table with data text column which stores data in JSON format.

$st = $pdo->prepare('SELECT data FROM test WHERE id = 8');
$st->execute();
$json = json_decode($st->fetchColumn(), 1);ctrl + c
$pdo->prepare

prepare given query to execute

$st->execute(

run query on the server

json_decode(

converts JSON string to associative array

$st->fetchColumn()

return single column (data in our case) value from resulting set

$json =

will store resulting JSON array


Usage example

<?php

$pdo = new PDO('mysql:host=localhost;dbname=test', 'usr', 'pwd');

$st = $pdo->prepare('SELECT data FROM test WHERE id = 8');
$st->execute();
$json = json_decode($st->fetchColumn(), 1);
print_r($json);
output
Array
(
    [some] => value
    [key] => okay
)