使用codeigniter php在数据库中将数组存储为json不工作

oxf4rvwz  于 2022-12-07  发布在  PHP
关注(0)|答案(1)|浏览(101)

我尝试使用codeigniter网站在数据库中存储一个数组作为json,如下所示:

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

该数组具有如下的值:

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
        )

)

数据库如下所示:


,但在数据库中,该值存储如下:

"Array"

有谁能告诉我这里出了什么问题吗,先谢谢了

rjee0c15

rjee0c151#

对我来说,这很有效:

$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"}]

相关问题