0

It's probably just a stupid mistake but I've built a tool to monitor tweet of a certain hash tag. However, when I try to save them to a database it only saves one tweet.

I have attached my code below.

foreach($results as $result) {

$tweetID = $result->id_str;
$userID = $result->from_user_id_str;
$tweetText = $result->text;
$tweet_time = strtotime($result->created_at);
$createdAt = date('Y-m-d H:i:s ',$tweet_time);

echo '<div>'.
     '<div class="tweet" >' . displayTweet($result->text),"\r\n" .
        '<div class="user">'. '<strong>Posted </strong>' . date('j/n/y H:i:s ',$tweet_time). '<strong> By </strong>' . 
        '<a rel="nofollow" href="http://twitter.com/' . $result->from_user. '">' . $result->from_user .
        '</div>' .
  '</div>'; 

// Execute query
mysql_select_db("dbname", $conn) or die(mysql_error()); 
$query = ("INSERT INTO ".$hashtag."(tweetID, userID, tweetText, createdAt) VALUES('$tweetID', '$userID', '$tweetText', '$createdAt')");
$output = mysql_query($query) or trigger_error(mysql_error()." in ".$query);
}
mysql_close($conn);
?>

How would I go about fixing this to get it to save every tweet? Thanks in advance

user1711576
  • 412
  • 10
  • 25
  • what is your code before the foreach statement? The code to get the tweet. Couldn't you just put the execute query inside of the query to get the tweets? – iHeff Dec 19 '12 at 16:55
  • i have just added all previous code to foreach statement – user1711576 Dec 19 '12 at 16:57
  • also i tried your suggestion and it doesn't make a change anything :( – user1711576 Dec 19 '12 at 17:01
  • please be aware that the `mysql_xxx()` functions are deprecated and not recommended for use. It is strongly recommended to switch to use either the `mysqli_xx()` functions or the PDO library. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for more info. – SDC Dec 19 '12 at 17:23
  • God, how much I hate questions being enormous block of code and then... help, please? – Tomasz Kowalczyk Dec 19 '12 at 17:31
  • var_dump the $results array to test if the problem is in your captured data or in your procedure to insert the data,. – luchosrock Dec 19 '12 at 17:33
  • @TomaszKowalczyk if you are going to comment on something please make it useful and not some pointless rant – user1711576 Dec 19 '12 at 17:38
  • @luchosrock yeah I did that, it isn't a problem with the captured data as it is all there – user1711576 Dec 19 '12 at 17:48

1 Answers1

0

There is nothing wrong with the code it turns out. It was to do with way it is parsing the tweet id and the table having a unique restraint on it

user1711576
  • 412
  • 10
  • 25