我插入了一行数据库表值,现在我想用json_encode回显它们,但我有以下错误:
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);
输出以上php代码:
[{"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)
数据库中的数据:(此数据由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_date": ["5555", "6666"],
"end_date": ["5555", "6666"],
"price_change": ["5555", "6666"]
},]
我该怎么办?
答案 0 :(得分:0)
你为什么这样做?
$data_rp = json_decode($row->residence_p, true);
您真的将json数据存储在数据库中吗?我对此表示怀疑,但如果你这样做了,请不要!这样做会破坏拥有列的数据库的全部目的。
答案 1 :(得分:0)
您应该在最后,
之前移除]
,否则数据无效JSON无法由json_decode()
正确解析。