0

I am trying to update my database table inside a foreach loop but it doesn't seem to work

I dont know where is the problem or what am doing wrong.

foreach($ids as $id_number=>$id)
{
    $request = "http://api.twitter.com/1/users/lookup.json?user_id=".$id."";
    $response = file_get_contents($request); 
    $ok = json_decode($response,true);/*print_r($ok);*/
    foreach($ok as $p)
    {
        $location=$p['location'];
        $query=mysql_query("UPDATE tweets SET location=$location 
                        WHERE from_user_id=$id");
    }
    if($query)
        echo'ok';
    else
        echo'no';
}       

It will be helpful if someone knows another way of doing this!

1
  • where and when do you check for PHP or MySQL errors? Commented Jul 29, 2012 at 16:37

1 Answer 1

2

try '$location' instead of $location :

$query=mysql_query("UPDATE tweets SET location='$location' 
                    WHERE from_user_id=$id");

keep in mind that The mysql_* functions are deprecated, it's better to use MySQLi or PDO

instead

Sign up to request clarification or add additional context in comments.

3 Comments

thank you.now working with the '$location'. i was using ".$location." this before
The mysql_* functions are deprecated, the PHP community recommends the use of either MySQLi or PDO to work with MySQL. See php.net/manual/en/mysqlinfo.api.choosing.php for more info.
Note: The mysql_* are not deprecated! They are no longer recommended and a future deprecation has been announced (no date set) but let's not confuse this with active deprecation and no strict deprecation errors will be thrown. E_DEPRECATED errors may be thrown as of 5.5 or 5.6, but to my knowledge this hasn't been decided yet. Regardless, point is well taken than people should stop using this extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.