Home >Backend Development >PHP Tutorial >mysql - 请教大神:PHP使用medoo不能将JSON插入数据库的问题?

mysql - 请教大神:PHP使用medoo不能将JSON插入数据库的问题?

WBOY
WBOYOriginal
2016-06-06 20:23:431386browse

<code><?php foreach (json_decode($_POST['data']) as $obj) {
        
        $arr = get_object_vars($obj);
        
        /*
            print_r($arr);
            print_r如下:
            Array
            (
                [UUID] => 10
                [content] => <p>内容</p>
            )    
        */
        
        //not work
        $database->insert("test",$arr);
        
        //It work
        $database->insert("test",[
            "UUID" => $arr['UUID'],
            "content" => $arr['content']
        ]);
        
    }</code>

刚接触PHP不久,Google了很久也不知道原因,很郁闷。希望大神能解答,谢谢

回复内容:

<code><?php foreach (json_decode($_POST['data']) as $obj) {
        
        $arr = get_object_vars($obj);
        
        /*
            print_r($arr);
            print_r如下:
            Array
            (
                [UUID] => 10
                [content] => <p>内容</p>
            )    
        */
        
        //not work
        $database->insert("test",$arr);
        
        //It work
        $database->insert("test",[
            "UUID" => $arr['UUID'],
            "content" => $arr['content']
        ]);
        
    }</code>

刚接触PHP不久,Google了很久也不知道原因,很郁闷。希望大神能解答,谢谢

mysql不支持直接插入数组形式的吧,你要保存成json格式的话,你得把数组 json_encode一下,组成一个json字符串保存至数据库

mysql 不能存储一个 array 对象。你得转换成json格式或者其他格式的字符串。顺便你在json_decode()的时候加上true是吧一个json字符串转换成array对象。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn