0

Im trying to get the values of a json file and insert it into a mysql database.

I getting all the object values from the json file but when i try to get the values of an array inside the array i simply insert into each row: "array"

I use following code (fixed for this purpose):

 <?php
    set_time_limit(0);

    function ApiCall($http_server, $params = array())
    {
        $query_string   =   '?';

        if(is_array($params) && count($params)){
            foreach($params as $k=>$v){
                $query_string .= $k.'='.$v.'&';
            }
            $query_string   =   rtrim($query_string,'&');
        }
        return  file_get_contents($http_server.$query_string);
    }


    function getVideos($page = 0, $category = false)
    {
        $http       =   'http://api.domain.com/';
        $call       =   'reference';


        $params     =   array(
            'output'    =>  'json',
            'data'      =>  $call,
            'page'      =>  $page
        );

        if($category){
            $params['category']     =   $category;
        }

        $response   =   ApiCall($http , $params);

        if ($response) {
            $json   =    json_decode($response);
            if(isset($json->code) && isset($json->message)){
                throw new Exception($json->message, $json->code);
            }
            return $json;
        }
        return false;
    }


    $page                       =   1;
    $pages                      =   2;

    while(true) {

        try{
            if($page > $pages)
                break;

            $videoResponse  =   getVideos($page);
            $videos         =   $videoResponse->videos;             

            $con=mysqli_connect("","","","");
            if (mysqli_connect_errno())
                {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }

            foreach($videos as $video) {
                $video_obj  =   $video->video;

                $video_id = $video_obj->video_id;
                $video_title = $video_obj->title;

                $video_tags = array();
                $video_tags[0] = $video_obj->tags-tag[0];
                $video_tags[1] = $video_obj->tags-tag[1];
                $video_tags[2] = $video_obj->tags-tag[2];
                $video_tags[3] = $video_obj->tags-tag[3];
                $video_tags[4] = $video_obj->tags-tag[4];
                $video_tags[5] = $video_obj->tags-tag[5];
                $video_tags[6] = $video_obj->tags-tag[6];
                $video_tags[7] = $video_obj->tags-tag[7];
                $video_tags[8] = $video_obj->tags-tag[8];
                $video_tags[9] = $video_obj->tags-tag[9];
                $video_tags[10] = $video_obj->tags-tag[10];

                $video_thumbnail = $video_obj->thumb;
                $video_embed = $video_obj->embed_url;
                $video_duration = $video_obj->duration;
                $video_url = $video_obj->url;

                $friendlyUrl = preg_replace('/^-+|-+$/', '', strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $video_title)));

                $result = mysqli_query($con,"INSERT INTO movies (movie_id, title, tags, stars, thumbnail, thumbnails, embed, length, location, friendlyUrl) 
                                             VALUES ('". $video_id ."','". $video_title ."', '". $video_tags ."', '". $video_stars ."', '". $video_thumbnail ."', '". $video_thumbnails ."', 
                                             '". $video_embed ."', '". $video_duration ."', '". $video_url ."', '". $friendlyUrl ."')") 
                or die(mysqli_error());

            }

            $pages          =   $videoResponse->count % 20 == 0 ? $videoResponse->count / 20 : floor($videoResponse->count) / 20;
            $page++;

            unset($videoResponse);
            unset($videos);

            sleep(2);

        } catch(Exception $e){
            // Something is wrong with the response. You should handle that exception.
        }   
    }
?>

When inserting $video_tags into the database i do not get the array values.

3
  • That means you're using an array in a string context. e.g. $foo = array(); echo $foo; will output the literal word Array. You don't show any actually useful mysql code (mysqli_query($con, "") is an utterly useless statement), so we can't help fix the actual problem. Commented Apr 9, 2015 at 14:28
  • Don't build a query string manually, especially not without proper encoding. Instead, use http_build_query(). Commented Apr 9, 2015 at 14:30
  • ive updated with the insert query Commented Apr 9, 2015 at 14:31

2 Answers 2

2

$video_tags is an array:

 $video_tags = array();
 $video_tags[0] = $video_obj->tags-tag[0];
 //                               ^ is that just a typo?
 // $video_tags[0] = $video_obj->tags->tag[0]; ???
 // etc.

So you cannot insert it in your query here:

$result = mysqli_query($con,"INSERT INTO movies (...) 
             VALUES (... , '". $video_tags ."',  ...)") 
                               ^^^^^^^^^^^ here

Ideally you would have a different table to store the tags but if you really must store them in one field, you need to serialize them or store them as for example a json string:

$result = mysqli_query($con,"INSERT INTO movies (...) 
             VALUES (... , '". serialize($video_tags) ."',  ...)") 

However, this will make it almost impossible to search / filter for tags so you really should store them in a different table where you link individual tags with individual movies.

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this is the problem, but in this line:

$video_tags[0] = $video_obj->tags-tag[0];

You seem to be either using a dash in a property name, or you forgot the dollar sign on the tag variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.