将数组插入数据库

时间:2013-08-13 06:17:23

标签: mysql arrays phpmyadmin

当我使用graph api时,我得到了这个结果。它采用数组格式

{
  "id": "216805086", 
  "name": "raj sharma", 
  "first_name": "raj ", 
  "last_name": "sharma", 
  "link": "https://www.facebook.com/raj.sharma.5", 
  "username": "raj .sharma.5", 
  "favorite_teams": [
    {
      "id": "198358615428", 
      "name": "Mumbai Indians"
    }, 
    {
      "id": "190313434323691", 
      "name": "Indian Cricket Team"
    }
  ], 
  "favorite_athletes": [
    {
      "id": "100787839962234", 
      "name": "Saina Nehwal"
    }
  ], 
  "gender": "male", 
  "email": "raj.discoverme@gmail.com", 
  "timezone": 5.5, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2013-08-13T06:01:17+0000"
}

我正在使用php语言和phpmyadmin数据库。现在我想将数组插入我的数据库。我应该为id,name,first_name,last_name,link,favorite_teams等制作一个列,还是应该为所有这些做一个列........ 如何将数组插入数据库

2 个答案:

答案 0 :(得分:1)

实际上这不是一个数组。这是JSON。在JSON中有两种格式,

  

JSONArray []

     

JSONObject {}

您正在将JSONObject作为输出。 PHP callerd JSONDecode中有一个函数。 通过这个你会得到主意。

答案 1 :(得分:0)

将Facebook应用数据存储在数据库中是违反Facebook政策http://developers.facebook.com/policy/

 $data = '{
  "id": "216805086", 
  "name": "raj sharma", 
  "first_name": "raj ", 
  "last_name": "sharma", 
  "link": "https://www.facebook.com/raj.sharma.5", 
  "username": "raj .sharma.5", 
  "favorite_teams": [
    {
      "id": "198358615428", 
      "name": "Mumbai Indians"
    }, 
    {
      "id": "190313434323691", 
      "name": "Indian Cricket Team"
    }
  ], 
  "favorite_athletes": [
    {
      "id": "100787839962234", 
      "name": "Saina Nehwal"
    }
  ], 
  "gender": "male", 
  "email": "raj.discoverme@gmail.com", 
  "timezone": 5.5, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2013-08-13T06:01:17+0000"
}';

//decode to get as php variable
$values = json_decode($data,true); //true to decode as a array not an object

$sql = "INSERT INTO TableName (id,name,first_name,last_name,link,username)
VALUES ('".$values['id']."','".$values['name']."','".$values['first_name']."','".$values['last_name']."','".$values['link']."','".$values['username']."')";
mysql_query($sql);

Json_decode()接受一个JSON编码的字符串并将其转换为PHP变量。