dsxi70423
2017-03-06 09:02
浏览 16
已采纳

将打印结果作为字符串发送到我的数据库

I want to store my result as just location name, i found the result as an array but it can't stored in my database, it print only a name 'array' in my database

try {
            $user6 = $fb->get('/me?fields=location');
            $user6 = $user6->getGraphNode()->asArray();

            //echo $user6->location->name;
            $user6->location->name; 
            echo "<pre/>";print_r($user6);


        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            session_destroy();
            // if access token is invalid or expired you can simply redirect to login page using header() function
            exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }

For save my result in my databse, my code is below:

$location= $user6 ['location'];

echo $location;

mysql_query("insert into newmember(location) values('$location')") or die(mysql_error());

my result is :

Array
(
    [location] => Array
        (
            [id] => 101889586519301
            [name] => Dhaka, Bangladesh
        )

    [id] => 1589273127757008
)
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujingya1166 2017-03-06 09:34
    已采纳

    Assuming you want to store just the location name in the database, you can do:

    $location = $user6['location']['name'];
    

    If you want to store the array itself, you can either json_encode the data or serialize it (note that you will need to do the reverse if you later want to use the data):

    // encode
    $location = json_encode( $user6['location'] );
    
    // decode
    $location = json_decode( $user6['location'] );
    

    or

    // encode
    $location = serialize( $user6['location'] );
    
    // decode
    $location = unserialize( $user6['location'] );
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题