MillerMedia
MillerMedia

Reputation: 3671

Facebook Graph - Gender info can't be retrieved in a while loop

I am using the Facebook Graph API to get the genders from a list of Facebook users. I have the user's IDs (they are current users of my app). For this, I won't need to use the API because gender does not require special permissions (even though I have obtained the basic info permissions anyhow). Basically I'm using a while loop to loop through all the rows on a MySQL table which has stored the FB ids that I grabbed when the user uses the app. When I do the json_decode code outside the while loop, it works great (but obviously only grabs the first instance in the table). Then when I put it in a loop, the page just won't load. It just sits and tries to load forever. Here's the code:

$sql = "SELECT * FROM fb_id ORDER BY timestamp DESC";
$result = $mysqli->query($sql);                     

while($row = mysqli_fetch_array($result)){
                    $jsonurl = "https://graph.facebook.com/".$row['fb_id'];
                    $json = file_get_contents($jsonurl,0,null,null);
                    $json_output = json_decode($json);
                    $user_gender = $json_output->gender;
                    echo $user_gender;
                    }

                    while($row = mysqli_fetch_array($result)){
                    echo "<a href='http://www.facebook.com/".$row['fb_id']."' target='_blank'><img src='http://graph.facebook.com/".$row['fb_id']."/picture/' /></a>";
                    }

You'll see the top bit of code is for the gender. The bottom loop is to display the user's profile pictures. That bottom loop works fine.

Any ideas why the call to the Facebook Graph would be getting stuck? Thanks!

Upvotes: 0

Views: 194

Answers (1)

C3roe
C3roe

Reputation: 96151

The bottom loop is to display the user's profile pictures. That bottom loop works fine.

The bottom loop does too.

Any ideas why the call to the Facebook Graph would be getting stuck?

The Graph API is fine, no worries.

You are looping over the Database query result once, to get the user details and echo out their gender.

After that, the DB query result is at its end, no more rows to fetch.

And that makes the condition of the second while loop become false at its first check, and so the loop content is not even executed once.

Why did you put a second while loop there anyway? You are accessing the same column value there as in the first loop – so why not put the contents of both code blocks into one code block after one while keyword?

Upvotes: 2

Related Questions