I have database values as JSON. I want to get values assigned to key like "name". How can I get those in PHP?.
<?php
include('db.class.php');
$sql = "select * from users where 1";
$obj = new db();
$stmt = $obj->conn->prepare($sql);
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($res,true);
echo $big = $json->name; // ??? ERROR
my output is
[
{"id":"1",
"name":"test1",
"username":"abc",
"email":"[email protected]",
"phone":"333"
},
{"id":"2",
"name":"test2",
"username":"def",
"email":"[email protected]",
"phone":"23232"}
]
$jsonis a string, not an object. And there's more than one row in the results, which one are you expecting$json->nameto return?echo '<pre>' . print_r($res, true) . '</pre>';so we can see what exactly is the structure of returned result$res[0]['name']json_encode()and see if parameter 2 has a TRUE option in there! Then you might like to swim over tojson_decode()just to say hello