0

I want insert in two-column database data woid and json_data, i can not getting value json_data for inset in database, How is it?

$data_end = json_decode('[
  {
    "woid": "2254271",
    "json_data": [
      {
        "code": "23",
        "date": "Tue, 03 Jan 2017 03:30 PM IRST",
        "temp": "68",
        "text": "Breezy"
      }
    ]
  },
  {
    "woid": "2254271",
    "json_data": [
      {
        "code": "23",
        "date": "Tue, 03 Jan 2017 03:30 PM IRST",
        "temp": "68",
        "text": "Breezy"
      }
    ]
  }
]');

foreach($data_end as $idx=>$val){
  $this->admin_model->UpdateData('weather', array('json_data' => $val->json_data), array('woid' => $val->woid));
}

And give me this error:

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_driver.php
Line Number: 1524


Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE weather SET json_data = Array WHERE woid = '2254271'
Filename: models/model.php
Line Number: 39

5
  • 2
    $val->json_data will give you array not string, so you can't insert Commented Jan 3, 2017 at 13:22
  • What do i do??? Commented Jan 3, 2017 at 13:23
  • actually it depended on what you actually want to insert from json_data? Or you want to insert json encoded data ? Commented Jan 3, 2017 at 13:24
  • ok, i use of json_encode. it worked. Commented Jan 3, 2017 at 13:26
  • good that you get it working Commented Jan 3, 2017 at 13:27

3 Answers 3

1

You are trying to parse an array in the query.

You could json_encode the array before inserting it.

$this->admin_model->UpdateData('weather', array('json_data' => json_encode($val->json_data)), array('woid' => $val->woid));
Sign up to request clarification or add additional context in comments.

Comments

0

$val->json_data is an array, you have to change it to string with json_encode( $val->json_data) before you insert to your db.

Comments

0

It will not work because you are trying to use PHP Array() in MySQl Query and it will not allowed you need to use json_encode() methos to save data.

you can update like that --

$json_data = json_encode($val->json_data);

$this->admin_model->UpdateData('weather', array('json_data' => $json_data), array('woid' => $val->woid));

Comments

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.