title: how to retrieve followers from twitter and store in database in php
body: <pre><code>&lt;?php
  
  
      session_start();
      require_once("twitter/twitteroauth/twitteroauth.php"); 
  
      $username="root";
      $password = "root";
      $database="gift";
      mysql_connect('localhost',$username,$password);
      $db_selected = mysql_select_db($database);
  
      if ( !$db_selected ) {
          die( "Unable to select database");
      }
      $queryy = "SELECT * FROM 'twitter'";
      $resultt = mysql_query($queryy);
  
          while ($row = mysql_fetch_object($resultt)) {
          $accesstoken=$row['twit_email'];
          $accesstokensecret=$row['password'];
  
          }
                 $cursor = -1;
                      $followers = array();
  
                do {
              $json = file_get_contents('https://api.twitter.com/1.1/search/tweets.json?cursor=' . $cursor);
              $accounts = json_decode($json);
              foreach ($accounts-&gt;users as $account)
                  $followers[$account-&gt;id_str] = $account-&gt;screen_name;
  
              $cursor = $accounts-&gt;next_cursor;
  
          } while ($cursor &gt; 0);
  
          // Now save in the database
          foreach($followers as $key =&gt; $value) {
  
              $res = $this-&gt; $database-&gt;prepare('SELECT COUNT(*) FROM `twit_contacts` WHERE twitID = :id');
              $res-&gt;execute(array('id' =&gt; $key));
              $rows = $res-&gt;fetch(PDO::FETCH_NUM);
  
              if (!$rows[0])
                  $this-&gt;$database-&gt;prepare('INSERT INTO `twit_contacts` SET twitID = :id, contactName = :name, dateContactFrom = NOW()')
                          -&gt;execute(array(':id' =&gt; $key, ':name' =&gt; $value));
              else
                  $this-&gt;$database-&gt;prepare('UPDATE `twit_contacts` SET contactName = :name, dateContactFrom = NOW() WHERE twitID = :id')
                          -&gt;execute(array(':id' =&gt; $key, ':name' =&gt; $value));
          }
  
  
  
  
  
  
  
  
  
      $query = "SELECT * FROM 'twit_contacts'";
      $result = mysql_query($query);
  
      $oldFollowers = array();
  
      while ($row = mysql_fetch_object($result)) {
          $oldFollowers[$row-&gt;contactID] = array('screen_name'=&gt;$row-&gt;screen_name, 'contactName'=&gt;$row-&gt;username);
          echo $oldFollowers['screen_name'];
      }
  
  
      mysql_close();
  ?&gt;
  </code></pre>