0

i am trying to store an array as json in database using codeigniter website like below:

$this->db->insert('orders', 
                array('orderstatus' => $orderstatus,  
                      'productname' => json_encode($product1)
                    )
    );

the array has values like below:

Array
(
    [0] => Array
        (
            [id] => 8
            [productname] => Couple Combo Sherwani
            [pimage] => _RJ_0149-min.jpg,_RJ_0342-min.jpg,_RJ_0115-min.jpg
            [jrp] => 6000
            [deposit] => 6000
            [size] => XL
            [duration] => 3
            [quantity] => 1
        )

)

the database looks like:

enter image description here enter image description here however in database the value is stored as below:

"Array"

can anyone please tell me what is wrong in here, thanks in advance

14
  • Please show us what is in $product1, a print_r($product1); for example Commented Jul 8, 2022 at 11:44
  • But why would you want to store what appears to be a name, so a single string, as a JSON String? Commented Jul 8, 2022 at 11:45
  • @RiggsFolly actually i want to store all the product info as json inside one field Commented Jul 8, 2022 at 12:01
  • Why would you use a relational database if you want to do this Commented Jul 8, 2022 at 12:04
  • @RiggsFolly its actually done in relational database, i need to fix only this thing Commented Jul 8, 2022 at 12:07

1 Answer 1

1

For me it works:

$products = [
    (object)[
        "name" => "Test product",
        "attribute1" => "Value1"
    ]
];

$this->db->table("orders")->insert([
    "name" => "Order for client",
    "info" => "Client want this order today",
    "products" => json_encode($products)
]); //In products column i have [{"name":"Test product","attribute1":"Value1"}]
Sign up to request clarification or add additional context in comments.

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.