anjel
anjel

Reputation: 1382

update database from foreach loop php not working

Hello am trying to update a column in my sql database using values from a foreach loop but am getting..Invalid argument supplied for foreach()

        foreach($ids as $id){
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/
    users/lookup.json?user_id=".$id."");
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    $contents = curl_exec ($ch);
    $ok=array();
    $ok=json_decode($contents,true);
    foreach($ok['results'] as $p){
        $location=$p['location'];
        $query=mysql_query("UPDATE tweets SET location=".$location."
         WHERE from_user_id=".$id."");
        if($query){
            print'ok';
        }else{print'sds';}
    }
    // close cURL resource, and free up system resources
    curl_close($ch);
    }

What am i doing wrong?

cheers people!!!!

Upvotes: 0

Views: 344

Answers (2)

James Williams
James Williams

Reputation: 4216

This is all you need. It only pulls the last tweet but it is a good start. Your call to twitter api is no longer working, put it in a browser window and you will find that it errors.

$contents = file_get_contents("http://api.twitter.com/users/".$id.".json");
$ok=json_decode($contents,true);

# For Testing Purposes
#echo '<pre>';
#var_dump($ok);
#echo '</pre>';

echo $ok['location'];

Upvotes: 1

A Person
A Person

Reputation: 1350

PHP arrays, whether associative or not, have a key and a value for each pair.

So, it should be foreach($ids as $id_number => $id) { as I'm guessing, since you didn't provide more information on the $ids array.

Upvotes: 0

Related Questions