php - Redeclare function in loop

107

I'm stumped as to how I would get this working. I'm using the twitteroauth.php library and I am grabbing the users token and token secret from a db query. Here is the code:

while($row = mysql_fetch_array($m))
{   
    $connection = new TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
    $consumerKey = "#####";
    $consumerSecret = "#####";
    $oauthToken = $row['oauth_token'];
    $oauthTokenSecret = $row['oauth_token_secret'];
    $userName = $row['username'];

    $query = mysql_query("select url from retweets order by rand() limit 1");
    $row = mysql_fetch_assoc($query);
    $retweet = basename($row['url']);

    $method = 'statuses/retweet/'.$retweet.'';
    twitterOAuth($method, $connection->post($method), $connection->http_code, $userName);
}

If there are multiple results from mysql, it loops through and says:

 Fatal error: Cannot redeclare random_from() (previously declared in /usr/share/nginx/www/twitteroauth/twitteroauth.php:199) in /usr/share/nginx/www/twitteroauth/twitteroauth.php on line 199

Since the$connection variable relies on$oauthToken and$oauthTokenSecret from the mysql query, I can't move it outside of the loop, so how would I make it so I can use it in each loop without redeclaring?

Thank you in advance!

Update: This is twitterOAuth

function twitterOAuth($method, $response, $http_code, $userName, $parameters = '') {
        if($http_code == 200){
            echo "retweeted successfully @".$userName."<br />";
        }else{
            echo "an error has occurred while retweeting @".$userName."";
            echo "<br />";
            print_r($response);
        }
}
183

Answer

Solution:

It needs to look like this...

while($row = mysql_fetch_array($m))
{   

$consumerKey = "#####";
$consumerSecret = "#####";
$oauthToken = $row['oauth_token'];
$oauthTokenSecret = $row['oauth_token_secret'];
$userName = $row['username'];

$connection = new TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);

$query = mysql_query("select url from retweets order by rand() limit 1");
$row = mysql_fetch_assoc($query);
$retweet = basename($row['url']);
$method = 'statuses/retweet/'.$retweet.'';

twitterOAuth($method, $connection->post($method), $connection->http_code, $userName);
}

You were declaring it before declaring the variables that you wanted to pass into it...

People are also looking for solutions to the problem: javascript - Sending ID to php via load function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.