0

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?

2
  • What is the error? What were you expecting to see? Commented Sep 25, 2011 at 21:54
  • I want get data from inside database, please see to code it is clear that, what i want. Commented Sep 25, 2011 at 22:02

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

I put in post data inside database.
Don't do that!!! Make columns start_date, end_date, and price_change, do not store json data in a database, you will regret it later if you do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.