Saving JSON string to MySQL database

前端 未结 3 478
一个人的身影
一个人的身影 2020-12-28 23:17

I have a JSON string with me

{\"name\":\"jack\",\"school\":\"colorado state\",\"city\":\"NJ\",\"id\":null}

I need it to be saved in the Dat

3条回答
  •  时光说笑
    2020-12-28 23:47

    Decode into an array and pass it in your mysql_query, the code below is not using mysql_real_escape_string or any other means of security, which you should implement.

    Assume $json is {"name":"jack","school":"colorado state","city":"NJ","id":null}

    $json_array = json_decode($json);
    

    You now have indexes in a php array, such as: $json_array['name']

    mysql_query("INSERT INTO student (name, school,city) VALUES('".$json_array['name']."', '".$json_array['school']."', '".$json_array['city']."') ") or die(mysql_error());  
    

提交回复
热议问题