tags:

views:

82

answers:

2

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/roadies/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'];
+2  A: 

this will happen if $posterInfo is actually an empty string ('').

can you var_dump($posterInfo) in the loop and check what it's doing...

jspcal
it indeed was an empty string.
amit
+4  A: 

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'];
Anthony
You probably want `is_array()` not `isArray()`.
pix0r
yeah, I realized that. I probably should have it check for if the array contains the 0 key.
Anthony
this is an excellent solution
jspcal
Hi, I have the same problem. The var_dump returns an empty string. I tried the solution suggested by Anthony, but no result. THe user name field is empty. @ amit: How did u solve the problem?
Angeline Aarthi