How to insert all the "story" (only) from the json formated data in the MySQL database using PHP. I would like to insert all the story in a single field. Please help.
array (
0 =>
array (
'story' => 'akhilesh shared Filmydrama\'s video.',
'created_time' =>
array (
'date' => '2016-02-12 14:05:15.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154545385862892',
),
1 =>
array (
'story' => 'akhilesh shared Kya Yehi Hain Acche Din?\'s video.',
'created_time' =>
array (
'date' => '2016-02-12 03:34:32.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154544563382892',
),
2 =>
array (
'story' => 'akhilesh shared a link.',
'created_time' =>
array (
'date' => '2016-02-12 03:28:09.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154544555572892',
),
3 =>
array (
'message' => 'R.I.P Jaihind',
'story' => 'akhilesh shared The Hindu\'s post.',
'created_time' =>
array (
'date' => '2016-02-11 07:46:59.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154542597202892',
),
4 =>
array (
'story' => 'akhilesh posted from Change.org.',
'created_time' =>
array (
'date' => '2016-02-11 05:09:08.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154542373792892',
),
5 =>
array (
'message' => 'Johnson & Johnson Finally Admits: Their Baby Products Contain Cancer-Causing Chemicals | ',
'created_time' =>
array (
'date' => '2016-02-11 01:38:33.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154542027992892',
),
6 =>
array (
'story' => 'akhilesh shared a link.',
'created_time' =>
array (
'date' => '2016-02-09 17:16:07.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154538723082892',
),
7 =>
array (
'story' => 'akhilesh shared The Guardian\'s video.',
'created_time' =>
array (
'date' => '2016-02-09 01:45:30.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154537304507892',
),
8 =>
array (
'story' => 'akhilesh shared The Frustrated Engineer\'s video.',
'created_time' =>
array (
'date' => '2016-02-08 01:18:59.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154534663442892',
),
9 =>
array (
'story' => 'akhilesh shared 24 Ghanta\'s post.',
'created_time' =>
array (
'date' => '2016-02-07 15:03:28.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154533473037892',
),
10 =>
array (
'story' => 'akhilesh shared a link.',
'created_time' =>
array (
'date' => '2016-02-07 14:54:39.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
),
'id' => '10154521329397892_10154533459592892',
),
)
$total_posts = array();
$array = json_encode($total_posts, true);
$my_arr = json_decode($array, true);
echo "<pre>";
$data = var_export($my_arr);
echo "</pre>";
$story = $data;
foreach( $my_arr as $row ) $story .= " {$row[story]}";
$story = trim( $story);
$stmt = $db->prepare("INSERT INTO users1 (name, token, message) VALUES (?, ?, ?)");
$stmt->bind_param("sss",$name, $accessToken, $story);
if ( !$stmt ) {
printf('errno: %d, error: %s', $db->errno, $db->error);
die;
}
else
{
$stmt->execute();
echo "New record created successfully !!";
}
$stmt->close();
$db->close();
The above code give me blank filed in database.
After EDIT is it possible to store 'id' and 'story' in one field, like id:123 story:'xyzabc' should be stored as 123 tab xyzabc newline in one field. So finally I can get all the story and corresponding story-id in one field where story and story-id are separated with tab
$story = $data;
foreach( $my_arr as $row )
$story .= " $row[message]\n";
$message_id = $data;
foreach( $my_arr as $row )
$message_id .= " $row[id]\n";
The above one is giving all the id's of the fetched data, but I would like to have the id of story only
$db->query()is validmysqlcall! If you are starting now with php, please don't use mysql: it's derecated and completely removed in php v. 7! So, when you will upgrade php, you will have to rewrite all your code! Use mysqli or PDO instead. You also have to bind yr query, because yr string is complex and can contains chars in conflict with mysql syntax (i.e. single quotation). See a basic PDO tutorial