在php中的数据库中的数组中插入数据数组

时间:2017-01-11 09:08:30

标签: php mysql arrays

我有一个数组followers_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
    )

现在我的数据库表包含三个字段id username和profile_pic_url 我想在数据库中插入这些数据。我试过但它只保存了三个数据,因为我在数组中有25个数据。

整个代码 这是我的完整代码。请告诉我哪里出错了。

  <?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";
  }

 ?>

这是我的三个数据表。 enter image description here

0 个答案:

没有答案