php string error
i am experiencing some weird behaviour here. i am using the following code to reference the facebook api.
$query = "SELECT msg, user_id, comment_time FROM comments WHERE aid = '$aid' ORDER BY comment_time DESC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)){
$uidval = $row->user_id;
$posterInfo = $facebook->api_client->users_getInfo($uidval, array('name', 'pic_square_with_logo', 'profile_url'));
$nameuser = $posterInfo[0]['name']; //this is line 50
$pic = $posterInfo[0]['pic_square_with_logo'];
$profile_url = $posterInfo[0]['profile_url'];
echo '<img src="'.$pic.'" />';
echo '<a href="'.$profile_url.'">'.$nameuser.'</a>';
echo '<br>';
echo $row->comment_time;
echo '<br>';
echo $row->msg;
}
}
it gives me this error:
Fatal error: Cannot use string offset as an array in /home/amitver/public_html/ro开发者_Go百科adies/comments.php on line 50
but surprisingly i am using the exact same code successfully at the top of my page. why this weird behaviour. this is the code at the top of page:
//connect to fB
$uid = $user_id;
$userInfo = $facebook->api_client->users_getInfo($user_id, array('name', 'pic_square'));
$nameuser = $userInfo[0]['name'];
$pic = $userInfo[0]['pic_square'];
I think that sometimes theusers_getInfo is returning an array, while other times it is returning a string. Probably it only returns a simple string if only one result is available.
Try this:
$nameuser = ($posterInfo[0]) ? $posterInfo[0]['name'] : $posterInfo['name'];
this will happen if $posterInfo is actually an empty string ('').
can you var_dump($posterInfo) in the loop and check what it's doing...
精彩评论