Jump to content

Array not being inserted into database


gg39

Recommended Posts

Hello all! I'm having a problem inserting an array into my database. My database is connected when I run the script but my table isn't being populated. Please help!

 

Here's my table structure followed by the php:

CREATE TABLE `demographic` (

  `uid` bigint(20) unsigned NOT NULL,

  `first_name` varchar(50) default NULL,

  `last_name` varchar(50) default NULL,

  `email` varchar(200) default NULL,

  `link` varchar(255) default NULL,

  `affiliations` varchar(255) default NULL,

  `birthday` varchar(50) default NULL,

  `current_location` varchar(200) default NULL,

  `education_history` varchar(500) default NULL,

  `work` mediumtext,

  `hometown_location` varchar(400) default NULL,

  `interests` varchar(200) default NULL,

  `locale` varchar(50) default NULL,

  `movies` varchar(500) default NULL,

  `music` varchar(500) default NULL,

  `political` varchar(200) default NULL,

  `relationship_status` varchar(100) default NULL,

  `sex` varchar(10) default NULL,

  `tv` varchar(200) default NULL,

  `status` tinyint(4) default NULL,

  `created` datetime default NULL,

  `updated` datetime default NULL,

  PRIMARY KEY  (`uid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

<?php
   
    $link = mysql_connect('host', 'user', 'pass'); 
    if (!$link) {
die('Could not connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db('database');
    if (!$db_selected) {
die ('Can\'t use beta : ' . mysql_error());
    }

echo 'Connected successfully';
mysql_close($link);
    
    include_once "fbmain.php";
    $config['baseurl']  =   "baseurl";

    //if user is logged in and session is valid.
    if ($fbme){
        //collect some data using legacy api
        $param  =   array(
            'method'     => 'users.getinfo',
            'uids'       => $fbme['id'],
            'fields'     => 'birthday_date, interests, locale, political, relationship_status, affiliations',
            'callback'   => ''
        );

        try{
            $info           =   $facebook->api($param);
        }
        catch(Exception $o){
            error_log("Legacy Api Calling Error!");
        }
        //using graph api
        //array data
        $workInfo       =   getWorkInfoAsString($fbme);
        $education      =   getEducationAsString($fbme);

        $moviesArr      =   $facebook->api("/me/movies");
        $musicArr       =   $facebook->api("/me/music");
        $televisionArr  =   $facebook->api("/me/television");

        //format some api data
        $movies         =   getArrayDataAsString($moviesArr['data']);
        $music          =   getArrayDataAsString($musicArr['data']);
        $television     =   getArrayDataAsString($televisionArr['data']);

        //data from legacy api
        $networks       =   '';
        if (!empty($info[0]['affiliations'])){
            $flag       =   true;
            foreach ($info[0]['affiliations'] as $item){
                if (!$flag)  $networks.= ' # ';
                $networks   .=  $item['name'];
                $flag   =   false;
            }
        }

        $now            =   date("Y-m-d G:i:s");
        $insData        =   array(
            'uid'                   =>  $fbme['id'],
            'first_name'            =>  $fbme['first_name'],
            'last_name'             =>  $fbme['last_name'],
            'email'                 =>  isset($fbme['email']) ? $fbme['email'] : '',
            'link'                  =>  $fbme['link'],
            'affiliations'          =>  $networks,
            'birthday'              =>  $info[0]['birthday_date'],
            'current_location'      =>  isset($fbme['location']['name']) ? $fbme['location']['name'] : '',
            'education_history'     =>  $education,
            'work'                  =>  $workInfo,
            'hometown_location'     =>  isset($fbme['hometown']['name']) ? $fbme['hometown']['name'] : '',
            'interests'             =>  $info[0]['interests'],
            'locale'                =>  $info[0]['locale'],
            'movies'                =>  $movies,
            'music'                 =>  $music,
            'political'             =>  $info[0]['political'],
            'relationship_status'   =>  $info[0]['relationship_status'],
            'sex'                   =>  isset($fbme['gender']) ? $fbme['gender'] : '',
            'tv'                    =>  $television,
            'status'                =>  '0',
            'created'               =>  $now,
            'updated'               =>  $now,
        );

        $this->db->insert('demographic', $insData);
    }

    function getWorkInfoAsString($fbme, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        if (empty($fbme['work'])) return '';

        foreach($fbme['work'] as $item){
            if ($flag)
                $info .= $partDelim;
            $flag   =   true;
            $info   .=  (isset($item['employer']['name']) ? $item['employer']['name'] : '' ). $delim .
                        (isset($item['location']['name']) ? $item['location']['name'] : '' ). $delim .
                        (isset($item['position'])         ? $item['position']['name'] : '' ). $delim .
                        (isset($item['start_date'])       ? $item['start_date']       : '' ). $delim .
                        (isset($item['end_date'])         ? $item['end_date']         : '' );
        }
        return $info;
    }

    function getEducationAsString($fbme, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        if (empty($fbme['education'])) return '';

        foreach($fbme['education'] as $item){
            if ($flag)
                $info .= $partDelim;
            $flag    =   true;
            $info   .=  (isset($item['school']['name']) ? $item['school']['name'] : '' ). $delim .
                        (isset($item['year']['name'])   ? $item['year']['name']   : '');
        }
        return $info;
    }

    function getArrayDataAsString($data, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        foreach($data as $item){
            if ($flag)
                $info .= $partDelim;
            $flag    =   true;
            $info   .=  $item['name'];
        }
        return $info;
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/220979-array-not-being-inserted-into-database/
Share on other sites

I just had a quick glance.. but i noticed that straight after you connect to the database you close the connection...

    $link = mysql_connect('host', 'user', 'pass'); 
    if (!$link) {



die('Could not connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db('database');
    if (!$db_selected) {



die ('Can\'t use beta : ' . mysql_error());
    }

echo 'Connected successfully';
mysql_close($link);

Sorry, I see it now. Is this all within a class? What does the $this->db->insert() method look like?

Hello thorpe,

$data = array(
               'title' => 'My title' ,
               'name' => 'My Name' ,
               'date' => 'My date'
            );

$this->db->insert('mytable', $data); 

That's supposed to produce: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

 

I just had a quick glance.. but i noticed that straight after you connect to the database you close the connection...

    $link = mysql_connect('host', 'user', 'pass'); 
    if (!$link) {



die('Could not connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db('database');
    if (!$db_selected) {



die ('Can\'t use beta : ' . mysql_error());
    }

echo 'Connected successfully';
mysql_close($link);

Hello Buddski,

Forgot to take that off before I posted it on here but even without that line there, it still doesn't populate the table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.