Jennifer Anthony
Jennifer Anthony

Reputation: 2277

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_date": ["5555", "6666"],
    "end_date": ["5555", "6666"],
    "price_change": ["5555", "6666"]
},]

What do i do?

Upvotes: 0

Views: 172

Answers (2)

rid
rid

Reputation: 63580

You should remove the , before the last ], otherwise the data is invalid JSON which cannot be correctly parsed by json_decode().

Upvotes: 0

Ariel
Ariel

Reputation: 26783

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.

Upvotes: 0

Related Questions