如何在mysql数据库中插入json数据的特定元素

时间:2016-02-13 12:23:31

标签: php mysql arrays json

如何使用PHP从MySQL数据库中的json格式化数据中插入所有“故事”(仅)。我想在一个字段中插入所有故事。请帮忙。

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();

的     上面的代码在数据库中给我空白。

编辑后 是否可以将'id''story'存储在一个字段中,例如id:123故事:'xyzabc'应存储为123 标签 xyzabc 换行符。最后,我可以在一个字段中获取所有故事和相应的故事ID,其中故事和故事ID用标签分隔

$story = $data;
    foreach( $my_arr as $row ) 

            $story .= " $row[message]\n";

    $message_id = $data;
    foreach( $my_arr as $row ) 

            $message_id .= " $row[id]\n";

上面提供了获取数据的所有id,但我想只有故事的id

1 个答案:

答案 0 :(得分:0)

$story = '';
foreach( $array as $row ) $story .= " {$row[story]}";
$story = trim( $story);

$story现在包含您可以添加到数据库的完整故事。

编辑(问题编辑后)

似乎你是随机编码的。注意自己的工作:

$total_posts = array();
$my_arr = json_decode($array, true);

echo "<pre>";
$data = var_export($my_arr);
echo "</pre>";

$stmt = $db->prepare("INSERT INTO users1 ((name, token, message) VALUES (?, ?, ?)");

在mysql查询中打开3个括号,但只关闭其中两个:在user1之后只能打开一个括号。
使用if( ! $stmt = $db->prepare(...) ) die("{$db->error}");检索prepare错误。

$stmt->bind_param($name, $accessToken, $lastname, $data);

您希望将哪些变量传递给bind_parameter?您想要在三个字段中插入值,然后传递四个参数?或$name包含绑定类型?我怀疑是这样的。请仔细阅读bind_param syntax,然后以类似的方式更改绑定:$stmt->bind_param('sss', $name, $accessToken, $data);“类似方式”表示您无需复制和粘贴,但需要了解语法并根据您的上下文使用它。

$result_insert = mysql_query($sql);

这是mysql_query是什么? 怀旧装饰?删除它。

if ($db->query($stmt) === TRUE) {

你真的读过“准备好的陈述”W3教程吗?在本教程中,有->query()方法?无处。使用预处理语句,您必须使用$stmt->execute()(不带参数)。

echo "New record created successfully !!";
} else {
    echo "Error: " . $stmt . "<br>" . $db->error;

您不能以这种方式打印出类mysqli_stmt($stmt)的对象:只需删除它并仅回显$db->error

}

$stmt->close();
$db->close()