sdiegolo78 Posted May 10, 2012 Share Posted May 10, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/ Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1344548 Share on other sites More sharing options...
scootstah Posted May 10, 2012 Share Posted May 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1344578 Share on other sites More sharing options...
sdiegolo78 Posted May 10, 2012 Author Share Posted May 10, 2012 i just tried as suggested and i am getting a message saying "Parse error: syntax error, unexpected '-', expecting '}'" referring to line $values[] = "('{$dm->sender_id}', '{$dm->sender_screen-name}')"; Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1344632 Share on other sites More sharing options...
scootstah Posted May 10, 2012 Share Posted May 10, 2012 Oops, it was a typo ... I put a "-" instead of a "_". Fixed: $values[] = "('{$dm->sender_id}', '{$dm->sender_screen_name}')"; Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1344635 Share on other sites More sharing options...
.josh Posted May 11, 2012 Share Posted May 11, 2012 also you should be sanitizing those values before inserting them into your db... Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1344645 Share on other sites More sharing options...
sdiegolo78 Posted May 13, 2012 Author Share Posted May 13, 2012 I am getting the error "Warning: json_decode() expects parameter 1 to be string, array given" referring to line foreach (json_decode($direct_message) as $dm) Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1345102 Share on other sites More sharing options...
scootstah Posted May 13, 2012 Share Posted May 13, 2012 Perhaps $OAuth->get() has decoded it already. I'm not familiar with the library. Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1345140 Share on other sites More sharing options...
sdiegolo78 Posted May 14, 2012 Author Share Posted May 14, 2012 "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; Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1345406 Share on other sites More sharing options...
sdiegolo78 Posted May 14, 2012 Author Share Posted May 14, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/262365-parsing-json-array-values/#findComment-1345442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.