Jump to content

parsing json array values


sdiegolo78

Recommended Posts

i am trying to get values from the json array that get direct messages from twitter but i am getting the message error "Parse error: syntax error, unexpected T_AS, expecting '(' " in relation to the line "foreach (array as $direct_message) {"

 

Also should i convert the array into a linear php array?

 


// create new instance
$OAuth = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);

$direct_message = $OAuth->get('https://api.twitter.com/1/direct_messages.json?count=1&page=');

function write_into_database ($direct_message) {

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

foreach(array as $direct_message) {
   mysql_query("INSERT INTO 'followers' ('user_id', 'user_alias'), VALUES ('{$direct_message->sender_id}', '{$direct_message->sender_screen_name}, INSERT INTO 'd_messages'('message_id', 'message'), VALUES ('{$direct_message->id}' ,'{$direct_message->text})");

}

write_into_database($direct_message);

Link to comment
Share on other sites

1. You shouldn't perform a query in a loop. You can do multiple inserts in a single query

http://www.electrictoolbox.com/mysql-insert-multiple-records/

 

2. Variables start with a $. You can't loop through a constant. You'll also have troubles accessing a constant named array. I'm not sure how to help you here without explaining how PHP, and programming syntax in general works. The best I can do is guide you to here:

http://php.net/manual/en/language.types.array.php

Link to comment
Share on other sites

Firstly the JSON will be an object, not an array. You will need to use json_decode to turn the JSON string into a PHP object, which you can then loop through.

 

Aside from that your insert syntax is incorrect.

 

Try this:

$values  = array();
$values2 = array();
foreach(json_decode($direct_message) as $dm)
{
$values[]  = "('{$dm->sender_id}', '{$dm->sender_screen-name}')";
$values2[] = "('{$dm->id}', '{$dm->text}')";
}

$query  = "INSERT INTO followers (user_id, user_alias) VALUES " . implode(',', $values);
$query2 = "INSERT INTO d_messages (message_id, message) VALUES " . implode(',', $values2);

mysql_query($query);
mysql_query($query2);

 

Also, there's really no need for a function here unless you are reusing it somewhere else too. It's just adding unnecessary overhead.

Link to comment
Share on other sites

"Perhaps $OAuth->get() has decoded it already." Yes, it did indeed since i got read of the json decode. I coded it down as "foreach($direct_message as $dm)".

 

However, when checking the database end, i saw it put it as "NULL" on the user_id field in the d_messages tables. Is that beacuse i set user_id as foreign key?

 


/* TwitterBot Database */

CREATE TABLE followers (
    user_id int, 
    user_alias varchar(20), 
    PRIMARY KEY (user_id)
) ENGINE=INNODB;

CREATE TABLE d_messages (
    message_id int, 
    user_id int, 
    message varchar (140), 
PRIMARY KEY (message_id), FOREIGN KEY (user_id) REFERENCES followers(user_id)
) ENGINE=INNODB;

Link to comment
Share on other sites

right, i sorted it myself, the database tables are fine. I just simply added user_id into d_messages. Thanks!

 


{
$values[]  = "('{$dm->sender_id}', '{$dm->sender_screen_name}')";
$values2[] = "('{$dm->id}', '{$dm->text}', '{$dm->sender_id}')";
}

$query  = "INSERT INTO followers (user_id, user_alias) VALUES " . implode(',', $values);
$query2 = "INSERT INTO d_messages (message_id, message, user_id) VALUES " . implode(',', $values2);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.