将用户推文插入mysql表

时间:2014-04-11 17:04:57

标签: php mysql sql twitter

我正在收到用户推文和提及并插入表格。完成此过程后,我将重定向到下一页。

对于少数用户,它可以正确存储和显示推文并重定向到下一页。但是对于少数用户来说,它会停留在第一页,我会收到推文并存入表格。我在最近几天试图克服这个问题。

任何人都可以告诉我这会导致什么问题吗?是否是因为它无法将数据(推文文本)存储到表中?

我执行插入操作有两个功能:

        function lookup($tweetid,$connection,$userid)
        {
                $tweets5 = $connection->get("https://api.twitter.com/1.1/statuses/retweets/".$tweetid.".json?count=1");
                //var_dump($tweets5);
                $json = json_encode($tweets5);
                foreach($tweets5 as $item)
                {
                        $text = $item->text;
            $text_id = $item->id;
//                        $user_id = $item->user->id;
                        $name = $item->user->name;
            $constant = 'retweet';
            $time = $item->created_at;
            $dt = \DateTime::createFromFormat('D M d H:i:s e Y', $time);
//          $dt = new \DateTime($time);
            $tweet_time = $dt->format('H:m:s');
            $tweet_dtm = $dt->format('Y:m:d');
            $year =  $dt->format('Y'); 
            $month =  $dt->format('m'); 

            echo $text."-".$text_id."-".$name."-".$time."-".$tweet_time.$tweet_dtm.$year.$month.$rt_count.$follower.$friend."<br>";
            echo "<br>";
            $inreplyto =  $item->in_reply_to_screen_name;

                     $follower = $item->user->followers_count;
            $rt_count = $item->retweet_count;
                        $friend = $item->user->friends_count;
                    $con = mysqli_connect('127.0.0.1', 'root', 'karim', 'karim');

            $text = mysql_real_escape_string($text);

                    if (mysqli_connect_errno())
                    {
                            echo "Failed to connect to MySQL: " . mysqli_connect_error();
                            return;
                    }

                    $insertQuery1 = "INSERT INTO twitter_retweet(`username`,`userid`,`tweet_text`,`text_id`,`time`,`month`,`year`,`date`,`user_follower_count`,`rt_count`,`constant`,`in_reply_to`) VALUES ('".$name."','".$userid."','".$text."','".$text_id."','".$tweet_time."','".$month."','".$year."','".$tweet_dtm."','".$follower."','".$rt_count."','".$constant."','".$inreplyto."')";

                    if (!mysqli_query($con,$insertQuery1))
                    {
                          die('Error: ' . mysqli_error($con));
                    //      echo "error";
                    }

//                        echo  "Text : $text <br>  ID : $user_id <br> Name : $name <br> Follower : $follower <br> Friends : $friend <br> ---";
                }


        }

此代码有什么问题吗?我不确定它是否在插入时卡在这里或什么。

第二个函数,它打印数据,但不会将$text存储到数据库中。为什么:

    foreach ($tweets1 as $item)
    {
        $text = $item->text;
        //echo $userid.$text;
        $text_id = $item->id;
        $constant = 'mention';
        $time = $item->created_at;
        //echo $time;
        //$dt = new DateTime('@' . strtotime($time));
        $dt = \DateTime::createFromFormat('D M d H:i:s e Y', $time);
        //var_dump($dt);
        $tweet_time = $dt->format('H:m:s');
        $tweet_dtm = $dt->format('Y:m:d');
        $year =  $dt->format('Y'); 
        $month =  $dt->format('m'); 
        $user_name = $item->user->name;
//      echo $year.$month.$user_name;
        $inreplyto =  $item->in_reply_to_screen_name;
        $rt_count = $item->retweet_count;
        $follower_count = $item->user->followers_count;


        echo $text."-".$text_id."-".$time."-".$tweet_time.$tweet_dtm.$year.$month.$user_name.$rt_count.$follower_count."<br>";
        echo "<br>";

        $con = mysqli_connect('127.0.0.1', 'root', 'karim', 'karim');           
        //$text = mysqli_real_escape_string($text);
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            return;
        }

        $insertQuery1 = "INSERT INTO twitter_mention(`username`,`userid`,`tweet_text`,`text_id`,`time`,`month`,`year`,`date`,`user_follower_count`,`rt_count`,`constant`,`in_reply_to`) VALUES ('".$twitteruser."','".$userid."','".$text."','".$text_id."','".$tweet_time."','".$month."','".$year."','".$tweet_dtm."','".$follower_count."','".$rt_count."','".$constant."','".$inreplyto."')";

        if (!mysqli_query($con,$insertQuery1))
        {
            die('Error: ' . mysqli_error($con));
        //  echo "error";
        }       
    }

这是重定向到下一页的最后代码:

echo '<form name="myForm" id="myForm"  action="start.php" method="POST">
<input style="display:none" name="userid" value="'.$userid.'" />

</form>

<script>
    function submitform()
    {
        document.getElementById("myForm").submit();
    }
    window.onload = submitform;
</script>
';

0 个答案:

没有答案