Monday, February 8, 2016

Storing multidimensional array in MySQL using PHP

Hi I am trying to do the same thing. I would like to save the Facebook user timeline details OR messages in MySQL database. Timeline details are visible in screen total posts are like this http://screenshot.co/#!/2dcbee03ff , if I want to see the 'messages' only I can see in the screen like http://screenshot.co/#!/635a038dd5 but not able to store in database. I tried with the following Code snippet but I am not getting expected result. I am getting "Array" in the message field http://screenshot.co/#!/9385792e8a . Please help me how to store all messages in single row for single user.

  1. $total_posts = array();
  2. $posts_response = $posts_request->getGraphEdge();
  3. if($fb->next($posts_response)) {
  4. $response_array = $posts_response->asArray();
  5. $total_posts = array_merge($total_posts, $response_array);
  6. while ($posts_response = $fb->next($posts_response)) {
  7. $response_array = $posts_response->asArray();
  8. $total_posts = array_merge($total_posts, $response_array);
  9. }
  10. //print_r($total_posts);
  11. foreach ($total_posts as $key ) {
  12. echo $key['message'].'<br>';
  13. }
  14. } else {
  15. $posts_response = $posts_request->getGraphEdge()->asArray();
  16. print_r($posts_response);
  17. }
  18. // storing in the database.
  19. $sql = "INSERT INTO users1 (name, token, message)
  20. VALUES ('{$name}', '{$accessToken}', '{$total_posts}')";
  21. if ($db->query($sql) === TRUE) {
  22. echo "New record created successfully !!";
  23. } else {
  24. echo "Error: " . $sql . "<br>" . $db->error;
  25. }
  26. $db->close();





The array structure is similar like this.

Array ( [0] => Array ( 
                      [story] => Anupam Jamatia shared The Frustrated Engineer's video. 
                      [created_time] => DateTime Object 
                         ( [date] => 2016-02-08 01:18:59.000000 
                           [timezone_type] => 1 
                           [timezone] => +00:00 ) 
                       [id] => 10154521329397892_10154534663442892 ) 
         [1] => Array ( 
                      [story] => Anupam Jamatia shared 24 Ghanta's post. 
                      [created_time] => DateTime Object 
                         ( [date] => 2016-02-07 15:03:28.000000 
                           [timezone_type] => 1 
                           [timezone] => +00:00 ) 
                       [id] => 10154521329397892_10154533473037892 ) 
        [2] => Array ( 
                      [story] => Anupam Jamatia shared a link. 
                      [created_time] => DateTime Object 
                         ( [date] => 2016-02-07 14:54:39.000000 
                           [timezone_type] => 1 
                           [timezone] => +00:00 ) 
                      [id] => 10154521329397892_10154533459592892 ) 
        [3] => Array ( 
                      [story] => Anupam Jamatia shared a link. 
                      [created_time] => DateTime Object 
                        ( [date] => 2016-02-05 01:31:52.000000 
                          [timezone_type] => 1 
                          [timezone] => +00:00 ) 
                       [id] => 10154521329397892_10154527172812892 ) 
)


Now is there any solution for this please.


Regards

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: