1

I have an array followers_array and inside that I have array followers. Now I want to insert all the data in database. I have tried this but this insert only three data and not all I want to insert data of an array which contain another array.

example 
Array
(
[0] => Array
    (
        [id] => 1766191289
        [username] => club_prichesok
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/13652273_1748726562036923_750729706_a.jpg
    )

[1] => Array
    (
        [id] => 1825482289
        [username] => recepturo
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/13269510_1062313797193421_2145524856_a.jpg
    )

[2] => Array
    (
        [id] => 1558269170
        [username] => bossbabesbrand
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/13652188_776051655868683_440592047_a.jpg
    )

[3] => Array
    (
        [id] => 3150718948
        [username] => aiyubpixster1
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/14597497_1333447786679600_2495297650182586368_a.jpg
    )

[4] => Array
    (
        [id] => 3126365193
        [username] => aiyubmunshi22
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/12960010_1525961487711830_121043771_a.jpg
    )

[5] => Array
    (
        [id] => 786675309
        [username] => a_trvn
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/14478472_200908700343641_544747813301387264_a.jpg
    )

[6] => Array
    (
        [id] => 3920937922
        [username] => campins09
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/14515676_1750967491822509_1220506517271216128_a.jpg
    )

[7] => Array
    (
        [id] => 3101694007
        [username] => apriyapari
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/12907442_1754066311508992_471114618_a.jpg
    )

[8] => Array
    (
        [id] => 3522063531
        [username] => soccergirrl.x
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/14566773_1820095541541963_7245353788171616256_n.jpg
    )

[9] => Array
    (
        [id] => 4307621933
        [username] => carmii_licious
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/15624190_382611378742665_5984516355464888320_a.jpg
    )

[10] => Array
    (
        [id] => 4120637171
        [username] => _cek.newphoto.or.unfollow_
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/15306790_705623506273370_4297264141430685696_a.jpg
    )

[11] => Array
    (
        [id] => 3019429401
        [username] => gungaoto
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/14624240_214178872360167_5880385332934868992_a.jpg
    )

[12] => Array
    (
        [id] => 4157340771
        [username] => hairstyle_makeup_style
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/15306541_1806174429600270_600008151021912064_a.jpg
    )

[13] => Array
    (
        [id] => 3514079761
        [username] => jemxma_esrn
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/14606945_318707198522914_4406240531315687424_a.jpg
    )

[14] => Array
    (
        [id] => 4374792130
        [username] => liindsay20
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/15876231_1774071109522171_5041779443538853888_a.jpg
    )

[15] => Array
    (
        [id] => 3669715747
        [username] => subhanallahh__
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/15534754_192323887898801_4040343084729368576_a.jpg
    )

[16] => Array
    (
        [id] => 3111272383
        [username] => irshad.pixster
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg
    )

[17] => Array
    (
        [id] => 1114256851
        [username] => pixsterstudio
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/11335149_984470211587963_1696181727_a.jpg
    )

[18] => Array
    (
        [id] => 4374336687
        [username] => putrysyafinazsyuhizam
        [profie_pic_url] => http://scontent-sin6-1.cdninstagram.com/t51.2885-19/s150x150/15875998_185924578548483_3898112154640842752_n.jpg
    )

now my table in database contain three fields id username and profile_pic_url I want to insert these data in database.I tried but it saves only three data where as I have 25 data in array.

whole code Here is my full code. please tell me where am I going wrong.

  <?php
  error_reporting(0);
  require('./vendor/autoload.php');
   $con = mysql_connect('localhost','root','') or die('Cannot connect to  the DB');
     mysql_select_db('insta',$con);
  $username = $_GET['user'];
  $pass = $_GET['pass'];

  if(!empty($pass) && !empty($username))
   {
   $i = new \InstagramAPI\Instagram(false);

  $i->setUser($username, $pass);

   if($i->settings->islogged() == false)
  {

    echo "You are not logged in.";
   }
  else
   {

    $a = $i->getUsernameId($username);

    $helper = null;
    $followers = [];

    $followers_array = array();

    do {
        if (is_null($helper)) {
            $helper = $i->getSelfUserFollowers();
        } else {
            $helper = $i->getSelfUserFollowers($helper->getNextMaxId());
        }

        $followers = array_merge($followers, $helper->getUsers());
    } while (!is_null($helper->getNextMaxId()));

    foreach ($followers as $follower) {
        $username = $follower->getUsername();
        $profie_pic_url = $follower->getProfilePicUrl();
        $id = $follower->getPk();
        $status = $i->userFriendship($id);

         $query = mysql_query("INSERT INTO `followers`  (username,profile_pic_url,follower_id)
          VALUES ('".$username."','".$profie_pic_url."','".$id."')");


       // if ($status->getFollowing() == false)
      //  {
         array_push($followers_array,array('id' => $id,'username' =>  $username,'profie_pic_url' => $profie_pic_url));
      //  }
    }
       if($query){
      echo "Data inserted </br>";
      }




   echo "<pre>";
   print_r($followers_array);



  }
 }
else
 {
     echo"username is required";
  }

 ?>

here is my table with three data. enter image description here

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Mudassar Saiyed
  • 1,146
  • 10
  • 20
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 11 '17 at 09:12
  • thank you Riggs for your valuable sugesstion. I will keep in mind – Mudassar Saiyed Jan 11 '17 at 09:13
  • 2
    And once you've learnt those libraries, read about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) so that you aren't vulnerable to [SQL injection](https://en.wikipedia.org/wiki/SQL_injection). – Phylogenesis Jan 11 '17 at 09:13
  • 1
    There are too many method calls to classes you must have written. There is no way we can tell from this amount of information what may have gone wrong with your data, since there cannot be any obvious syntax errors. You need to track the state of your arrays through the entire code, printing their contents out at each iteration and compare them to the expected data. – Shadow Jan 11 '17 at 09:19
  • so shall i post my whole code here?? if you want I can post it here. – Mudassar Saiyed Jan 11 '17 at 09:20
  • It's possible that some character in the data you're trying to insert causes the problem. Try to escape al especial characters with something like: mysqli_real_escape_string or mysql_real_escape_string (deprecated) – MarioZ Jan 11 '17 at 10:50
  • still getting three values. where as i am getting all 25 value when I use print_r – Mudassar Saiyed Jan 11 '17 at 10:54
  • Only 3 inserts in the database and all 25 in `$followers_array`? I would try to insert the 4th manually with Phpmyadmin to see if the sql works (if you didn't do ti yet) It's what I do to check whenever I have problems with queries. – MarioZ Jan 11 '17 at 11:04
  • ok i will try your trick yes.. when I use print_r($followers_array) i get 25 data and when I insert using above script I get on three data. – Mudassar Saiyed Jan 11 '17 at 11:11
  • yes I am able to insert using phpmyadmin @MarioZ – Mudassar Saiyed Jan 11 '17 at 11:19

0 Answers0