1 Replies - 998 Views - Last Post: 27 April 2012 - 03:43 PM Rate Topic: -----

#1 sdiegolo78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-April 12

writing php array values into mysql database

Posted 27 April 2012 - 03:28 PM

i have retrieved the direct messages from my twitter account and i want to store senderid, sender screen name and text in mysql database tables, in order to be read by the Artificial intellingece markup language engine so it can reply accordingly. After i execute the program, i checked the database and no row has been entered from the array and no error message either.

Here's my code:


$connection = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);

// Get direct message data 
$content = $connection ->get('https://api.twitter.com/1/direct_messages.json');

echo '<pre>',print_r($content,true),'</pre>';

function write_messages_to_database() {

require_once('connect_to_database');

$conn = mysql_connect("127.0.0.1", "Diego", "frafra") or die(mysql_error());
mysql_select_db('bot', $conn) or die(mysql_error());

foreach ($content as $directmessage) {
   mysql_query("INSERT INTO 'twitter_senders' ('sender_id', 'sender_name'), VALUES ('{$content->id}', '{$content->sender_screen_name}')");
   $directmessage_id = mysql_last_insert_id();
   foreach ($directmessage->text as $text) {
        mysql_query("INSERT INTO 'message_senders' ('message'), VALUES ('{$text->text}')");
   } 

 }
}




Below, the array from which i just want to extract the values in id,sender_screen_name and text. Also, the array has three objects but i just pasted in one for the example.

I am not getting any error message, it's just the program is not writing into the database

What do you think?


Array
(
    [0] => stdClass Object
        (
            [sender_id] => 374504554
            [recipient_screen_name] => Infobot2012
            [recipient_id] => 428575951
            [recipient] => stdClass Object
                (
                    [id] => 428575951
                    [profile_image_url] => 
                    [url] => 
                    [created_at] => Sun Dec 04 22:56:38 +0000 2011
                    [followers_count] => 3
                    [default_profile] => 1
                    [profile_background_color] => C0DEED
                    [lang] => en
                    [utc_offset] => 
                    [name] => Diego Canale
                    [profile_background_image_url] => 
                    [location] => 
                    [profile_link_color] => 0084B4
                    [listed_count] => 0
                    [verified] => 
                    [protected] => 
                    [profile_use_background_image] => 1
                    [is_translator] => 
                    [following] => 
                    [description] => 
                    [profile_text_color] => 333333
                    [statuses_count] => 10
                    [screen_name] => Infobot2012
                    [profile_image_url_https] => 
                    [time_zone] => 
                    [profile_background_image_url_https] => 
                    [friends_count] => 14
                    [default_profile_image] => 1
                    [contributors_enabled] => 
                    [profile_sidebar_border_color] => C0DEED
                    [id_str] => 428575951
                    [geo_enabled] => 
                    [favourites_count] => 0
                    [profile_background_tile] => 
                    [notifications] => 
                    [show_all_inline_media] => 
                    [profile_sidebar_fill_color] => DDEEF6
                    [follow_request_sent] => 
                )

            [id_str] => 194550124767227905
            [sender] => stdClass Object
                (
                    [id] => 374504554
                    [profile_image_url] => 
                    [url] => 
                    [created_at] => Fri Sep 16 12:46:20 +0000 2011
                    [followers_count] => 19
                    [default_profile] => 1
                    [profile_background_color] => C0DEED
                    [lang] => en
                    [utc_offset] => 
                    [name] => Diego Canale
                    [profile_background_image_url] => 
                    [location] => Dublin
                    [profile_link_color] => 0084B4
                    [listed_count] => 0
                    [verified] => 
                    [protected] => 
                    [profile_use_background_image] => 1
                    [is_translator] => 
                    [following] => 1
                    [description] => 
                    [profile_text_color] => 333333
                    [statuses_count] => 92
                    [screen_name] => sdiegolo
                    [profile_image_url_https] => 
                    [time_zone] => 
                    [profile_background_image_url_https] => 
                    [friends_count] => 79
                    [default_profile_image] => 
                    [contributors_enabled] => 
                    [profile_sidebar_border_color] => C0DEED
                    [id_str] => 374504554
                    [geo_enabled] => 
                    [favourites_count] => 1
                    [profile_background_tile] => 
                    [notifications] => 
                    [show_all_inline_media] => 
                    [profile_sidebar_fill_color] => DDEEF6
                    [follow_request_sent] => 
                )

            [created_at] => Mon Apr 23 22:15:38 +0000 2012
            [id] => 1.9455012476723E+17
            [sender_screen_name] => sdiegolo
            [text] => Hello Infobot!
        )

This post has been edited by Atli: 27 April 2012 - 03:34 PM
Reason for edit:: Added the second [code] tags. Far easier to read this way.


Is This A Good Question/Topic? 0
  • +

Replies To: writing php array values into mysql database

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,983
  • Joined: 08-June 10

Re: writing php array values into mysql database

Posted 27 April 2012 - 03:43 PM

Is that your entire script?

That code would not do any database operations for two reasons:

  • The write_messages_to_database function is never called. Therefore the code inside that function is never executed.

  • Even if said function is being executed, the $content variable is defined outside the function, and will not be available inside the function's scope. Read up on Variable Scope in the Manual to see exactly how that works.


Also, aside from that, the require_once('connect_to_database'); line inside the function doesn't make much sense. Is that an actual script file, without an extension? - The fact that you go on to connect to the database in the next two lines suggests that this require_once call shouldn't really be there.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1