将数组插入数据库

时间:2022-12-04 15:39:20

I am getting this result when I am using graph api . it is in array format

当我使用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"
}

I am working in a php language and phpmyadmin database . Now i want to insert the array into my database . Should i make a column for id , name , first_name ,last_name,link,favorite_teams etc or should i make a one column for all of this ........ how toinsert tha array into the database

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

2 个解决方案

#1


0  

Storing facebook app data in a database is against Facebook policy http://developers.facebook.com/policy/

将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() takes a JSON encoded string and converts it into a PHP variable.

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

#2


1  

Actually this is not an array. This is JSON. In JSON there are two formats,

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

JSONArray [ ]

JSONArray []

JSONObject { }

JSONObject {}

You are getting the JSONObject as your output. There is a function in PHP callerd JSONDecode. Go through this you will get idea.

您将获得JSONObject作为输出。 PHP callerd JSONDecode中有一个函数。通过这个你会得到的想法。

#1


0  

Storing facebook app data in a database is against Facebook policy http://developers.facebook.com/policy/

将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() takes a JSON encoded string and converts it into a PHP variable.

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

#2


1  

Actually this is not an array. This is JSON. In JSON there are two formats,

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

JSONArray [ ]

JSONArray []

JSONObject { }

JSONObject {}

You are getting the JSONObject as your output. There is a function in PHP callerd JSONDecode. Go through this you will get idea.

您将获得JSONObject作为输出。 PHP callerd JSONDecode中有一个函数。通过这个你会得到的想法。