Problem with echo values from database
I inserted on a row of database table values, now I want echo them with json_encode, but I have following error:
PHP:
$name_tour = $this->input->post('tour_name');
$query = $this->db->query("SELECT * FROM tour_foreign WHERE name LIKE '$name_tour' ORDER BY id desc");
$data = array();
foreach ($query->result() as $row) {
$data_rp = json_decode($row->residence_p, true);
$data[] = array(
'residence_p' => $data_rp,
);
}
echo json_encode($data);
echo '<p>';
var_dump($data);
Output above php code:
[{"residence_p":null}] // This is output of json_encode($data)
array(1) { [0]=> array(1) { ["residence_p"]=> NULL } } // This is output of var_dump($data)
Data in database:(this data insert by json_encode)
[{
"start_date": ["1111", "2222"],
"end_date": ["1111", "2222"],
"price_change": ["1111", "2222"]
}, {
"start_date": ["3333", "444"],
"end_date": ["3333", "4444"],
"price_change": ["3333", "4444"]
}, {
"start_da开发者_开发百科te": ["5555", "6666"],
"end_date": ["5555", "6666"],
"price_change": ["5555", "6666"]
},]
What do i do?
You should remove the ,
before the last ]
, otherwise the data is invalid JSON which cannot be correctly parsed by json_decode()
.
Why are you doing this?
$data_rp = json_decode($row->residence_p, true);
Did you really store json data in your database? I doubt it, but if you did DON'T! Doing that defeats the entire purpose of having a database with columns.
精彩评论