droos02800 2014-10-10 08:06
浏览 47
已采纳

使用PHP将Json数据发送到MySQL

I am new to the Json and PHP. This is sample but I am trying to put the Json data into the MySQL using PHP with this code I was able to input the Json data to the MySQL except the Json data ["@context": "dfg2"] can not put this data into the MySQL rest of the data like ["empid": "M01AH"], ["name":"Ricky Jones"] were able to put these value in the mySQL using PHP

Question is how can I put ["@context": "dfg2"] data into the MYSQL using PHP? I know the using @ in the PHP script is not that good..

$json_obj = '{
      "empid": "M01AH",
      "personal": {
           "name":"Ricky Jones",
           "gender": "Male"
      },
      "profile": {
           "designation": "VP",
           "department": "Marketing",
              "@context": "dfg2"
      }
 }';
//If I take out @ in the [ "@context": "dfg2"] I can put the data in to the MYSQL


//convert to stdclass object
$emp_data = json_decode($json_obj);





//store the element values into variables
$id = $emp_data->empid;
$name = $emp_data->personal->name;
$gender = $emp_data->personal->gender;
$designation = $emp_data->profile->designation;
$department = $emp_data->profile->department;
$e = $emp_data->profile->@context;      //If I take out @ I can put the data in to the MYSQL How can I leave @ to make the data into the MYSQL




//insert values into mysql database  
$sql="INSERT INTO tbl_employee (`empid`, `empname`, `gender`, `designation`, `department`, `@context`)
VALUES ('$id', '$name', '$gender', '$designation', '$department', '$e')";

In the My SQL I was able to put the empid, empname, gender, designation, department data except the @context

please help me with this using @ mark in the php and mysql.

  • 写回答

2条回答 默认 最新

  • doucuo4413 2014-10-10 08:11
    关注

    You need to escape the property name like so:

    $e = $emp_data->profile->{'@context'}; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用