php - Why does the loop execute only once? -
i have following small code manipulate tweets data. expect loop iterate 10 times. however, happens iterates once , exits, with no sign of error relating mysql or otherwise.
$query = "select data tweets `screen_name` = 'username' limit 10"; $tweetsq = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli)); $tweets = mysqli_fetch_assoc($tweetsq); $tweets_count = mysqli_num_rows($tweetsq); echo $tweets_count . '<br />'; //see output $count = 0; foreach ($tweets $raw_tweet) { $tweet = json_decode($raw_tweet); $tweet_id = $tweet->id_str; $is_reply = (isset($tweet->in_reply_to_screen_name) && strlen($tweet->in_reply_to_screen_name) > 0) ? 1 : 0; $is_retweet = (isset($tweet->retweeted_status) && $tweet->retweeted_status != '') ? 1 : 0; $entity_holder = array(); $has_hashtag = $has_url = $has_mention = $has_media = 0; foreach ($tweet->entities $type => $entity) { if (is_array($entity) && count($entity) < 1) { //continue; } else { $entity = array_pop($entity); switch ($type) { case 'hashtags' : $has_hashtag = 1; break; case 'urls' : $has_url = 1; break; case 'user_mentions' : $has_mention = 1; break; case 'media' : $has_media = 1; break; default : } } } echo 'updating recorde... <br />'; $query = "update tweets set is_reply='" . $is_reply . "' , is_retweet='" . $is_retweet . "', has_hashtag='" . $has_hashtag . "', has_url='" . $has_url . "', has_mention='" . $has_mention . "', has_media='" . $has_media . "' tweet_id='" . $tweet_id . "'"; $result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli)); var_dump($result); //see output $count++; echo '<br />'; } echo $count;
output:
10 //this value of $tweets_count updating recorde... bool(true) //the result of update query 1 //the value of $count @ end of script. should 10
mysqli_fetch_assoc
fetches a single row associative array key column name , value column value. correct way use iterate on result set until fetch returns null
, indicating there no more rows fetch:
while ($row = mysqli_fetch_assoc($tweetsq)) { $raw_tweet = $row["data"]; $tweet = json_decode($raw_tweet); $tweet_id = $tweet->id_str; # etc...
Comments
Post a Comment