将打印结果作为字符串

时间:2017-03-06 09:02:19

标签: php mysql facebook-graph-api

我想将结果存储为位置名称,我发现结果是一个数组,但它不能存储在我的数据库中,它只在我的数据库中打印一个名称'array'

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;
        }

为了保存我的数据库中的结果,我的代码如下:

$location= $user6 ['location'];

echo $location;

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

我的结果是:

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

    [id] => 1589273127757008
)

1 个答案:

答案 0 :(得分:0)

假设您只想在数据库中存储位置名称,您可以执行以下操作:

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

如果您想存储数组本身,您可以json_encode数据或serialize它(请注意,如果您以后想要使用数据,则需要执行相反操作):< / p>

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

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

// encode
$location = serialize( $user6['location'] );

// decode
$location = unserialize( $user6['location'] );